setState 不适用于 React 和 Meteor 中的 UI 更改

setState did not work for UI change in React and Meteor

我正在尝试使用按钮来切换 UI 显示。当用户点击按钮时,它会显示相应的UI。并在用户再次点击时隐藏它。我使用 setState 来切换 "showItemBank" 的值,但是 UI 只显示一次,然后又回到默认的不可见状态。我错过了什么重要的东西吗?

这是我的应用程序的 jsx,使用 Meteor 和 React。

App = React.createClass ({

mixins: [ReactMeteorData],

getInitialState() {
    return {
      showItemBank: false
    }
},

onItemBankClick(event) {
    this.setState({
        showItemBank : ! this.state.showItemBank
    });

},

render() {
    var classItemBank = ""; 
    var classNewQuestion = ""; 
    if(this.state.showItemBank === false){ 
        classItemBank = "displayNone"; 
    } 
    if(this.state.showItemBank === true){ 
        classItemBank = "displayContent"; 
    }

    return (
        <div className="container">
            <header>
                <form className="new-task" >
                    <div>
                        <button onClick={this.onItemBankClick} className="menu">Select From the Item Bank</button>
                        <button className="menu">Add New Questions</button>
                    </div>
                    <div className={classItemBank}> Hi i am from ItemBank </div>    
                </form>

            </header>
        </div>
    );
}

  });

试试这个

onItemBankClick(event) {
    event.preventDefault();
    this.setState({
        showItemBank : ! this.state.showItemBank
    });

}

按钮默认会尝试提交页面

onItemBankClick(event) {

 // stop the event bubbling to parent, in this case <form>
 event.stopPropagation(); 

 // prevent default browser action
 event.preventDefault();

 this.setState({
    showItemBank : ! this.state.showItemBank
 });

}