简体   繁体   English

reactjs setState不改变状态

[英]reactjs setState not changing state

I have a function(onClickAddSection), when its called, it should set the state to empty string but it doesn't do that at all. 我有一个函数(onClickAddSection),当它被调用时,它应该将状态设置为空字符串,但是它根本不这样做。 Please take a look at the code and tell me what im doing wrong thank you. 请看一下代码,然后告诉我即时消息做错了什么,谢谢。

class AddNewSectionForm extends Component {
constructor(props) {
    super(props);
    this.state = {
        sectionName: '',
        validation:false
    };
    this.onSectionNameChange = this.onSectionNameChange.bind(this);
    this.onClickAddSection = this.onClickAddSection.bind(this);

}

onSectionNameChange(event) {
    if(this.state.validation==false){
        this.setState({validation:true});
    }
    this.setState({sectionName: event.target.value});
}

onClickAddSection(event) {
    event.preventDefault();
    this.props.saveSection(this.state.sectionName);
    this.setState({sectionName:'',validation:false});
}

render() {
    return (
            <div>
                <TextInput name="newSection" label="Section Name :"
                           onChange={this.onSectionNameChange}
                           value={this.state.sectionName}
                           error = {this.state.validation==true&& this.state.sectionName.length==0?'Enter Section Name':''}/>
                <AddCloseButtons add = {this.onClickAddSection}
                                 close = {this.props.closeCharm}
                                 />
            </div>
    );
}

} }

the onClickAddSection function doesn't set the sectionName back to ''. onClickAddSection函数不会将sectionName设置回。

AddCloseButton: AddCloseButton:

const AddCloseButtons = ({add,close}) => {
return (
    <div className="form-group">
        <button className="btn btn-primary" style={{width: '40%', border:'solid black 1px'}} onClick={add}>Add</button>
        <button className="btn btn-secondary" style={{width: '40%', float: 'right',border:'solid black 1px'}} onClick={close}>Close</button>
    </div>);

}; };

Are you using Redux? 您在使用Redux吗? if so, this.props.saveSection makes any Ajax Call?. 如果是这样,this.props.saveSection会进行Ajax调用吗? If so, could be that you update the local state with setState and then your Ajax response re updates it the received value? 如果是这样,可能是您使用setState更新了本地状态,然后您的Ajax响应又将其更新为接收到的值了吗?

Just try and see the functional version of setState . 只需尝试看看setState的功能版本即可。

this.setState(function (state, props) {
 return {
  sectionName:'',
  validation:false
 }
});

It seems that when you click the button, onSectionNameChange and onClickAddSection happen at the same time and will have conflict (because the name is set to blank means its name is being changed ^^), so how about not setting state for sectionName in onSectionNameChange : 似乎当您单击按钮时, onSectionNameChangeonClickAddSection会同时发生,并且会发生冲突(因为名称设置为空意味着其名称已更改^^),那么如何不在onSectionNameChange中设置sectionName的onSectionNameChange

onSectionNameChange(event) {
    if(this.state.validation==false){
        this.setState({validation:true});
    }
    //this.setState({sectionName: event.target.value});
}

I guess so, please post here some errors if any, thanks 我想是的,请在此处发布一些错误(谢谢)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM