onClick不会呈现新的react组件。

onClick不会呈现新的react组件。,第1张

onClick不会呈现新的react组件

您可能希望有一个状态组件,该状态组件在单击按钮之后在按钮旁边显示另一个组件。您需要做的就是跟踪按钮是否被单击:

class MyComponent extends React.Component {  constructor(props) {    super(props);    this.state = {      showComponent: false,    };    this._onButtonClick = this._onButtonClick.bind(this);  }  _onButtonClick() {    this.setState({      showComponent: true,    });  }  render() {    return (      <div>        <Button onClick={this._onButtonClick}>Button</Button>        {this.state.showComponent ?<NewComponent /> :null        }      </div>    );  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5614960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存