Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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
Javascript 如何在react中传递状态?_Javascript - Fatal编程技术网

Javascript 如何在react中传递状态?

Javascript 如何在react中传递状态?,javascript,Javascript,我需要在单击submit时获得“+”或“-”结果,但我根本不知道如何处理它。这是我的代码,在单击“提交”之前显示结果 类应用程序扩展了React.Component{ 构造函数(){ 超级(); this.state={counter:0}; } render(){ 返回( + {this.state.counter} - 提交 ); } 增量=()=>{ 这是我的国家({ //计数器:this.state.counter+1 }) } 减量=()=>{ 这是我的国家({ //selected

我需要在单击submit时获得“+”或“-”结果,但我根本不知道如何处理它。这是我的代码,在单击“提交”之前显示结果

类应用程序扩展了React.Component{
构造函数(){
超级();
this.state={counter:0};
}
render(){
返回(
+
{this.state.counter}
-
提交
);
}
增量=()=>{
这是我的国家({
//计数器:this.state.counter+1
})
}
减量=()=>{
这是我的国家({
//selectedFunction=-1??
})
}
提交=(selectedFunction)=>{this.setState({
计数器:this.state.counter+{selectedFunction}
})};
}
ReactDOM.render(,document.getElementById(“app”)

您应该在构造函数中绑定递增和递减函数,并将方法作为onClick prop传递

class App extends React.Component {
  constructor() {
    super();
    this.state = {counter: 0};
    this.increment = this.increment.bind(this);
    this.decrement = this.decrement.bind(this);
  }

  render() {
    return (
      <div>
        <button onClick={this.increment}>+</button>
        <output>{this.state.counter}</output>
        <button onClick={this.decrement}>-</button>
      </div>
    );
  }

  increment() {
    this.setState(function(prevState){
      return { counter: prevState.counter + 1 }
    });
  }

  decrement() {
    this.setState(function(prevState){
      return { counter: prevState.counter - 1 }
    });
  }
}
ReactDOM.render(<App />, document.getElementById("app"));
类应用程序扩展了React.Component{
构造函数(){
超级();
this.state={counter:0};
this.increment=this.increment.bind(this);
this.decrement=this.decrement.bind(this);
}
render(){
返回(
+
{this.state.counter}
-
);
}
增量(){
this.setState(函数(prevState){
返回{counter:prevState.counter+1}
});
}
减量{
this.setState(函数(prevState){
返回{counter:prevState.counter-1}
});
}
}
ReactDOM.render(,document.getElementById(“app”);
这个应该可以:

class App extends React.Component {
  constructor() {
    super();
    this.state = {counter: 0, tempCounter: 0};
    this.increment = this.increment.bind(this);
    this.decrement = this.decrement.bind(this);
      this.submit = this.submit.bind(this);
  }

  render() {
    return (
      <div>
        <button onClick={this.increment}>+</button>
        <output>{this.state.counter}</output>
        <button onClick={this.decrement}>-</button>
        <button onClick={this.submit}>submit</button>
      </div>
    );
  }
  submit(){
       const tempCount = this.state.tempCounter;
       this.setState({counter: tempCounter})
  }
  increment() {
    const tempCount = this.state.tempCounter;
    this.setState({
       tempCounter: tempCount + 1 
    });
  }

  decrement() {
    const tempCount = this.state.tempCounter;
    this.setState({
       tempCounter: tempCount - 1 
    });
  }
}
ReactDOM.render(<App />, document.getElementById("app"));
类应用程序扩展了React.Component{
构造函数(){
超级();
this.state={counter:0,tempCounter:0};
this.increment=this.increment.bind(this);
this.decrement=this.decrement.bind(this);
this.submit=this.submit.bind(this);
}
render(){
返回(
+
{this.state.counter}
-
提交
);
}
提交(){
const tempCount=this.state.tempCounter;
this.setState({counter:tempCounter})
}
增量(){
const tempCount=this.state.tempCounter;
这是我的国家({
tempCounter:tempCount+1
});
}
减量{
const tempCount=this.state.tempCounter;
这是我的国家({
tempCounter:tempCount-1
});
}
}
ReactDOM.render(,document.getElementById(“app”);

ok,我编辑了代码以使其看起来更清晰,所有的工作都由按钮完成,但我想让“提交”来完成结果,而不是只做“+”结束“-”,我该怎么做?