var _this=this; 不生效

import React from 'react'

class Tab4  extends React.Component {
  constructor(){
    super();
    this.state={
      yes:true,
      shownum:1,
      boxText:1,
      boxStyle:1
    }
  }
  show(x){
    this.setState({
      shownum:x,
      boxText:x,
      boxStyle:x
    });
  }
  render () {
    let boxText=["我是11111111","我是22222222","我是33333333"];
    var _this=this;
    let styles={
      showStyle:[{
                    width:"300px",
                    height:"100px",
                    background:"rgb(49, 194, 97)",
                    display:_this.state.shownum==1 ? "block" : "none"
                  },
                  {
                    width:"300px",
                    height:"100px",
                    background:"rgb(230, 142, 90)",
                    display:_this.state.shownum==2 ? "block" : "none"
                  },
                  {width:"300px",
                    height:"100px",
                    background:"rgb(201, 103, 198)",
                    display:_this.state.shownum==3 ? "block" : "none"
                  }
      ]
    }
    return(
      <div>
        <button onClick={this.show.bind(this,1)}>show1</button>
        <button onClick={this.show.bind(this,2)}>show2</button>
        <button onClick={this.show.bind(this,3)}>show3</button>
        <div style={styles.showStyle[this.state.boxStyle]}>
                    {boxText[this.state.boxText-1]}
        </div>
      </div>
    )

  }
}

export default Tab4;

到底怎么回事啊?不报错但就是不显示
如果点button1或2会是这样图片描述

如果点button3又会是一点没有样式
图片描述

阅读 3.1k
3 个回答

我觉得是你这句话:
<div style={styles.showStyle[this.state.boxStyle]}>
没有把boxStyle - 1

你用的是ES6的方式,改成function声明试试

新手上路,请多包涵

var _this=$(this);
这是es6的规范

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题