我怎样才能改变我的状态

时间:2018-05-12 11:04:58

标签: reactjs

当我点击按钮时,状态应该被更改。我可以改变我的状态。初始点击状态为false,每当点击按下的按钮应为真。



<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
class Static extends Component{
 constructor(props){
 super(props);
 this.state = {
   clicked_status:false
 }

render(){
   return(
       <div className="input-group mb-3 topA">
           <input className="input2" id="name" type="text" placeholder="  Name " /><br/>
           <input className="input2 mt-10" id="mail" type="text" placeholder="  Email "/><br/>
           <input className="input2 mt-10" id="passwrd" type="password" placeholder="  Password "/><br/>
           <span id="error_passwrd" className="text-danger fs12"></span>
           <input className="input2 mt-10" id="cpassword"  type="password" placeholder="  Confirm Password "/><br/>
           <span id="error_cpassword" className="text-danger fs12"></span>
           <a ><button type="submit" id="submit" className="button7 btn mt-10 bckgrnC" >Sign Up</button></a>
       </div>        
                   
   )
   }

 }
&#13;
&#13;
&#13;

我是新人的反应,我怎样才能改变我的状态,什么是改变我的反应状态的过程

1 个答案:

答案 0 :(得分:-1)

在反应中,很容易改变状态。

1.您只需编写一个函数并绑定该函数

2.将onClick功能写入按钮

3.通过setState改变状态

假设在你的例子中

constructor(props) {
 super(props);
 this.ChangeStatus= this.ChangeStatus.bind(this);
}

这是写入更改状态的函数。我们可以通过setState

更改状态
  

注意:始终使用setState()方法修改组件的状态。

ChangeStatus: function(e) {
{
    this.setState({ clicked_status: true });
}

将onClick操作添加到按钮

<button onClick={this.ChangeStatus}>
相关问题