Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 我已经通过react.js类组件制作了一个计数器,但它不是递增的,我不知道它有什么问题_Reactjs_Components - Fatal编程技术网

Reactjs 我已经通过react.js类组件制作了一个计数器,但它不是递增的,我不知道它有什么问题

Reactjs 我已经通过react.js类组件制作了一个计数器,但它不是递增的,我不知道它有什么问题,reactjs,components,Reactjs,Components,我不知道这个代码有什么问题 我也做了装订,但仍然没有任何效果 <div id="root"> </div> <script type="text/babel"> class Counter extends React.Component{ constructor(props){ super(props); this.state={count:0}; this

我不知道这个代码有什么问题 我也做了装订,但仍然没有任何效果

<div id="root">
</div>
    <script type="text/babel">
    class Counter extends React.Component{
        constructor(props){
            super(props);
            this.state={count:0};
            this.clickHandler=this.clickHandler.bind(this);
        };

        clickHandler(){
            this.setState((prevState,props)=> {count: prevState.count+5});
            };

        render(){
            return <button onClick={this.clickHandler}> {this.state.count}</button>
        };
    };
    var element=<Counter />
    ReactDOM.render(element, document.getElementById('root'));

    </script>

类计数器扩展了React.Component{
建造师(道具){
超级(道具);
this.state={count:0};
this.clickHandler=this.clickHandler.bind(this);
};
clickHandler(){
this.setState((prevState,props)=>{count:prevState.count+5});
};
render(){
返回{this.state.count}
};
};
变量元素=
render(元素,document.getElementById('root');

您是否收到任何错误?您的单击处理程序必须如下所示

clickHandler() {
    this.setState((prevState,props)=> ({count: prevState.count+5}));
};
检查对象前面添加的
和对象后面添加的

this.setState({count:this.state.count+5})
 clickHandler() {
    this.setState(
      {
        count: this.state.count + 5
      }
    );
  }