onClick doesn't render new react component.

前端 未结 4 1379
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 17:37

I\'m new to react world and I have line like this:


and on cli

4条回答
  •  星月不相逢
    2020-11-27 18:43

    You probably want to have a stateful component that shows the other component next to the button after it was clicked. All you need to do is track whether the button was clicked:

    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 (
          
    {this.state.showComponent ? : null }
    ); } }

提交回复
热议问题