【问题标题】:How to change component inside of render with a button click?如何通过单击按钮更改渲染内部的组件?
【发布时间】:2019-10-21 17:30:56
【问题描述】:

我是编程新手,尤其是 ReactJS。其实我遇到了问题。

我只是想换个页面。对我来说这听起来很简单,但它是一个没有乐趣的 2 天 Google-Search-Nothing-Found-Adventure。

我制作了一个名为<Frontpage />的组件 它在开始时直接呈现,但我想通过单击按钮来更改它。

这是我的代码:

import React from 'react';
import './App.css';
import Frontpage from './Frontpage'
import Question from './components/Question'


class App extends React.Component {
  constructor() {
    super()
    this.state = {
      showComponent: true,
    }
  }

  handleClick() {
    if (this.state.showComponent) {
      return <Question />
    } else {
      console.log("Something went wrong!")
    }
    // console.log("The button was clicked!")
    // document.getElementById('page')
    // document.getElementsByClassName('App-Header')
  }

  render() {

    return (
        <div className="App">
          <header className="App-header">
            <div id="page">
              <Frontpage />
            </div>
              <button id="button" onClick={this.handleClick}>Los geht's</button>
          </header>
        </div>
    );
  }
}


export default App;

当我点击它时,它总是说:“TypeError: Cannot read property 'state' of undefined”

我尝试了很多,但没有任何效果。

【问题讨论】:

标签: javascript reactjs button components


【解决方案1】:

试试这样的。将 handleClick 函数更改为箭头函数并根据状态呈现组件。

更新:请注意我使用了Fragment 来包装buttonQuestion 元素/组件

import React,{Fragment} from 'react';
import './App.css';
import Frontpage from './Frontpage'
import Question from './components/Question'


class App extends React.Component {
  constructor() {
    super()
    this.state = {
      showComponent: false,
    }
  }

  handleClick =()=> {
     this.setState({showComponent:true})
  }

  render() {

    return (
        <div className="App">
          <header className="App-header">
            <div id="page">
              {this.state.showComponent? <Fragment><Question /><button id="button" onClick={this.handleClick}>Los geht's</button></Fragment> : <Frontpage /> }
            </div>
          </header>
        </div>
    );
  }
}


export default App;

【讨论】:

  • 谢谢!!这有很大帮助并且奏效了。只剩下一个问题:该按钮仍在我的下一页上。编辑:我重用了这个按钮,点击@&lt;Frontpage /&gt;它变成了``` 组件上的“下一步”。
  • @minternational 你设法让它工作了吗?如果它的工作,请接受答案
  • 好的,谢谢。很抱歉等待这个。我是新来的,不知道流程。
【解决方案2】:

您必须将handleClick 绑定到您的class component,或者在其他情况下,使用箭头函数: 要在state 底部的constructor绑定方法,请添加以下内容:

this.handleClick = this.handleClick.bind(this);

要使用 箭头函数,请将方法的语法更改为:

this.handleClick = () => {
    if (this.state.showComponent) {
      return <Question />
    } else {
      console.log("Something went wrong!")
    }
  }

【讨论】:

    【解决方案3】:
    import React from 'react';
    import './App.css';
    import Frontpage from './Frontpage'
    import Question from './components/Question'
    
    
    class App extends React.Component {
      constructor() {
        super()
        // Start with a hide component
        this.state = {
          showComponent: false,
        }
        //  Binding your function
        this.handleClick = this.handleClick.bind(this);
      }
    
      handleClick() {
        const { showComponent } = this.state;
        // Use state to trigger a re-render
        this.setState({
          showComponent: !showComponent;
        })
      }
    
      render() {
        const { showComponent } = this.state;
        return (
          <div className="App">
            <header className="App-header">
              <div id="page">
                {
                  // Conditionally render a component
                  showComponent
                  ? (<Question />)
                  : (<Frontpage />)
                }
              </div>
              <button id="button" onClick={this.handleClick}>Los get's</button>
            </header>
          </div>
        );
      }
    }
    
    
    export default App;
    

    【讨论】:

    • 感谢您的帮助,但它会在 &lt;Frontpage /&gt; 组件上额外渲染 &lt;Question /&gt; 组件。
    【解决方案4】:

    试试这个。

    import Question from './components/Question'
    
    
    class App extends React.Component {
      constructor() {
        super()
        this.state = {
          showComponent: true,
        }
      }
    
      handleClick() {
        this.setState({ showComponent: true });
      }
    
      render() {
        const { showComponent } = this.state;
        return (
            <div className="App">
              <header className="App-header">
                <div id="page">
                  <Frontpage />
                </div>
                { showComponent ? <Question /> : null }
                  <button id="button" onClick={this.handleClick.bind(this)}>Los geht's</button>
              </header>
            </div>
        );
      }
    }
    
    export default App;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多