Reactjs 无法在内部显示状态变量<;h1>;

Reactjs 无法在内部显示状态变量<;h1>;,reactjs,jsx,Reactjs,Jsx,shoot是我试图在函数shooter中更改的变量,稍后在中显示 shooter(){ this.setState({shoot:Math.floor(Math.random()*Math.floor(3))}); console.log(“你好”); } render(){ 返回( {这个州,开枪} 这个,射手,开枪 ); } } 不会随着状态的改变而改变,难道状态不会随着shooter()的触发而改变吗?它不更新 非常感谢您的帮助。:-) 换行 shooter() { this.setS

shoot
是我试图在函数shooter中更改的变量,稍后在
中显示

shooter(){
this.setState({shoot:Math.floor(Math.random()*Math.floor(3))});
console.log(“你好”);
}
render(){
返回(
{这个州,开枪}
这个,射手,开枪
);
}
} 
不会随着状态的改变而改变,难道状态不会随着
shooter()
的触发而改变吗?它不更新

非常感谢您的帮助。:-)

换行

shooter() {
this.setState({ shoot: Math.floor(Math.random() * Math.floor(3)) });
console.log("hello");
}

render() {
return (
  <div>
    <h1>{this.state.shoot}</h1>
    <button onClick={() => this.shooter}>shoot it</button>
  </div>
);
}
} 
this.shooter}>开枪

this.shooter()}>开枪吧
更改行

shooter() {
this.setState({ shoot: Math.floor(Math.random() * Math.floor(3)) });
console.log("hello");
}

render() {
return (
  <div>
    <h1>{this.state.shoot}</h1>
    <button onClick={() => this.shooter}>shoot it</button>
  </div>
);
}
} 
this.shooter}>开枪

this.shooter()}>开枪吧

在构造函数中绑定类方法
shooter
,以便像这样使用它
onClick={this.shooter}

你可以找到进一步的解释

导出默认类player扩展React.Component{
构造函数(…参数){
超级(…args);
此.state={
拍摄:0
};
this.shooter=this.shooter.bind(this);
}
render(){
返回(
{这个州,开枪}
开枪
);
}
}

在构造函数中绑定类方法
shooter
,以便像这样使用它
onClick={this.shooter}

你可以找到进一步的解释

导出默认类player扩展React.Component{
构造函数(…参数){
超级(…args);
此.state={
拍摄:0
};
this.shooter=this.shooter.bind(this);
}
render(){
返回(
{这个州,开枪}
开枪
);
}
}
()=>this.shooter()
()=>this.shooter()
<button onClick={() => this.shooter}>shoot it</button>
<button onClick={() => this.shooter()}>shoot it</button>
 export default class player extends React.Component {
      constructor(...args) {
        super(...args);
        this.state = {
          shoot: 0
        };
        this.shooter = this.shooter.bind(this);
      }

      render() {
        return (
            <div>
                <h1>{this.state.shoot}</h1>
                <button onClick={this.shooter}>shoot it</button>
            </div>
        );
      }
    }