简体   繁体   English

渲染反应组件onClick

[英]Render react component onClick

I'm super new to react but excited about its potential. 我是超级新人,但对其潜力感到兴奋。 Still getting to grips with the fundamentals of it all so any explanation would be greatly appreciated. 仍然要掌握它的基本原理所以任何解释都会非常感激。

I'm looking to render an 'About' component as the user clicks a button in the 'Nav' component (with the aim to toggle this later down the line). 当用户点击“导航”组件中的按钮时,我希望渲染一个“关于”组件(目的是稍后将其切换到该行)。

I've attempted to do it in the simplest way I can think of, but this is obviously very wrong: 我试图用我能想到的最简单的方式来做,但这显然是非常错误的:

class Nav extends React.Component {

 renderAbout() {
  return (
   <About />
 );
 }

render() {
 return (
  <div className="Nav">
    <div className="Button-Container">
    <div className="Nav-Text About-Button">
      <h2 onClick={() => this.renderAbout()}>About</h2>
    </div>
    </div>
  </div>
  );
 }
}

Would this have something to do with updating the 'state' of the About component? 这是否与更新About组件的“状态”有关?

Thanks in advance. 提前致谢。

You can use state to define if imported component About has to be rendered or not. 您可以使用state来定义是否必须呈现导入的组件About

class Nav extends React.Component {

  state = {
    isAboutVisible: false,
  }

  render() {
   return (
    <div className="Nav">
      <div className="Button-Container">
      <div className="Nav-Text About-Button">
        <h2 onClick={() => this.setState({ isAboutVisible: true }) }>About</h2>
      </div>
      </div>
      { this.state.isAboutVisible ? <About /> : null }
    </div>
    );
   }
}

Yes, you have to change state of the component. 是的,您必须更改组件的状态。 Changing the state will automatically rerender your component. 更改状态将自动重新呈现组件。 In your example it should be something like: 在你的例子中,它应该是这样的:

class Nav extends React.Component {

 state = {
   showAbout: false; // initial state
 }

 renderAbout = () => {
  if (!this.state.showAbout) return '';

return (
   <About />
 );
 }

// ES6 priavte method syntax
handleButtonClick = () => {
 this.setState({showAbout: true});
}

render() {
 return (
  <div className="Nav">
    <div className="Button-Container">
    <div className="Nav-Text About-Button">
      <h2 onClick={this.handleBtnClick}>About</h2>
      {this.renderAbout()}
    </div>
    </div>
  </div>
  );
 }
}

You could also consider using for example this package: https://www.npmjs.com/package/react-conditions 您也可以考虑使用此软件包: https//www.npmjs.com/package/react-conditions

Also, remember that there is a rule that each method which listen for an event should start with the "handle" word. 另外,请记住,每个监听事件的方法都应以“句柄”字开头。 Like in may example. 就像在可能的例子中。

You currently do not have "About" component in actual view, you just render it somewhere out there, in the void! 您目前在实际视图中没有“关于”组件,只是将它渲染到那里的某个地方,在虚空中!

To properly render a component you have to specify its place in JSX expression. 要正确呈现组件,您必须在JSX表达式中指定其位置。 Also, as one of the easiest solutions) you probably want to toggle it. 此外,作为最简单的解决方案之一,您可能想要切换它。 So that translates to something like this: 所以转换为这样的东西:

 constructor(props){
     super(props)
     this.state={toggle:false}
 }

renderAbout(toggle) {
 if(toggle)
   return <About />
 else return null;
}

render() {
return (
  <div className="Nav">
    <div className="Button-Container">
      <div className="Nav-Text About-Button">
        <h2 onClick={() => this.setState({toggle: !toggle})}>About</h2>
      </div>
    </div>
    {this.renderAbout(this.state.toggle)}
  </div>
  );
 }
}

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

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