简体   繁体   English

如何在 Reactjs 中调用组件?

[英]how to call components in Reactjs?

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      activeTab: "add",
      items: [],
    }
  }
  handleClick(activeTab) {
    switch (activeTab) {
      case 'add': return <Add/>
      case 'list': return <List />
      case 'pay': return <Pay />
    }
  }
  render() {
    return (
      <div className="App btn-group">
        <Button onClick={this.handleClick.bind(this, 'add')}><Add /></Button>
        <Button onClick={this.handleClick.bind(this, 'list')}><List /></Button>
        <Button onClick={this.handleClick.bind(this, 'pay')}><Pay /></Button>
      </div>
    )
  }
}
export default App;

I wish through this code in reactjs, When I onClick on an 'add' button it shows me the Add components but unfortunately it does not work.我希望通过 reactjs 中的这段代码,当我点击“添加”按钮时,它会向我显示添加组件,但不幸的是它不起作用。 On the other hand in my switch when I do an alert () or a console.log () instead of returning my components;另一方面,当我执行 alert () 或 console.log () 而不是返回我的组件时,在我的 switch 中; it works well as it should.它应该运行良好。

the <Add /> List and Pay are components and I have imported into the app.js. <Add /> List 和 Pay 是组件,我已经导入到 app.js 中。

there are several issues with your code.您的代码有几个问题。

  1. if you want to show the components based on your active Tab you have to maintain the state variable .如果要根据活动选项卡显示组件,则必须维护状态变量。 I see you have set your initial state activeTab : "add" but trying to render <Add /> component inside a button .我看到您已设置初始状态activeTab : "add"但试图在 button 内呈现<Add />组件。 that's why it doesn't work as expected .这就是它不能按预期工作的原因。 you have to render it separately .你必须单独渲染它。

  2. your handleClick function is supposed to do something .您的 handleClick 函数应该做一些事情。 not return something.不回东西。 so here if you return your components inside switch that won't work.所以在这里,如果您将组件返回到开关中,则无法正常工作。 you can change the state here.您可以在此处更改状态。

  3. you can use the conditional rendering feature of react to render the desired component based on your active tab as shown below.您可以使用 react 的条件渲染功能根据您的活动选项卡渲染所需的组件,如下所示。

here is the complete component code that should solve the problems i mentioned above这是应该解决我上面提到的问题的完整组件代码

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      activeTab: "add",
      items: [],
    }
  }

  handleClick(activeTab) {
    this.setState({activeTab})
  }

  render() {
    return (
      <div>

 
        <div className="App btn-group">
            <Button onClick={this.handleClick.bind(this, 'add')}> Add </Button> 
            <Button onClick={this.handleClick.bind(this, 'list')}> List </Button>
            <Button onClick={this.handleClick.bind(this, 'pay')}> Pay </Button>  
        </div>

        {this.state.activeTab === 'add' &&  <Add /> }
        {this.state.activeTab === 'list' &&  <List /> }
        {this.state.activeTab === 'pay' &&  <Pay /> }
      </div>
    )
  }
}
export default App;

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

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