简体   繁体   English

如何获取父App.js中的navbar子组件state?

[英]How to obtain navbar child component state in parent App.js?

TL;DR: I'd like to pass click result from Navbar component to its parent App component. TL;DR:我想将 Navbar 组件的点击结果传递给它的父 App 组件。

I have a Navbar.js which is rendered in App.js , and the navbar has several tabs on it.我有一个在Navbar.js中呈现的App.js ,导航栏上有几个选项卡。 In App.js there are also several Card.js component rendered based on click event on the navbar, eg a user clicks 'Food' on navbar, App.js should show all 'Food' cards.App.js中,还有几个基于导航栏上的点击事件呈现的Card.js组件,例如,用户点击导航栏上的“食物”, App.js应该显示所有“食物”卡片。 Now in App.js , I'd like to know which tab has been clicked on navbar.现在在App.js中,我想知道在导航栏上单击了哪个选项卡。 Here are some code:下面是一些代码:

Navbar.js (the child component) Navbar.js(子组件)

class Navbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {currentTab: null};
    this.findTab = this.findTab.bind(this);
  }
  findTab(){
    this.setState({currentTab: 'Food'}) // here onClick the state of navbar is set to 'Food'
  }
  render(){
    return (
      <Navbar>
        <Navbar.item onClick={() => this.findTab(tab)}>
        </Navbar.item>
      </Navbar>
    )
  }
}

App.js (the parent component): App.js(父组件):

class App extends React.Component {
  constructor(props){
    super(props);
    this.state = {currentCategory: null};
    this.renderCards = this.rendercards.bind(this);
  }
  renderCards(){
    const category = 'Food' // here I need to know on Navbar.item, user clicked and set the state to 'Food', so I can make category = 'Food'
    this.setState({currentCategory: category});
  }
  render(){
    return(
      <Navbar />
      <Card currentCategory={this.state.currentCategory} />
    )
  }
}

As you can see I have made the Navbar.js state to 'Food' on click, I'm not sure if this is the correct way to pass data to its parent.如您所见,我在点击时将Navbar.js state 设置为“食物”,我不确定这是否是将数据传递给其父项的正确方法。

Well, As per my understanding this situation can be easily handled by simply passing renderCards() as props in Navbar.js component.好吧,根据我的理解,这种情况可以通过简单地将renderCards()作为道具传递到 Navbar.js 组件中来轻松处理。

For more details on props and how to use them please go through the Reactjs docs.有关道具及其使用方法的更多详细信息,请通过 Reactjs 文档 go。 Components and props组件和道具

Below are the changes you need to do to achieve App.js render right card.以下是实现App.js渲染正确卡片所需的更改。

In Navbar.js you have to take use this.props.renderCards('Food') Onclick. In place of 'Food' you can pass diffrent categories.Navbar.js中,您必须使用this.props.renderCards('Food') Onclick。代替“Food”,您可以传递不同的类别。

class Navbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {currentTab: null};
    this.findTab = this.findTab.bind(this);
  }
//we do not need findTab method to achieve your renderCard().
  findTab(){
    this.setState({currentTab: 'Food'}) // here onClick the state of navbar is set to 'Food'
  }
  render(){
    return (
      <Navbar>
        <Navbar.item onClick={() => this.props.renderCards('Food')}>
        </Navbar.item>
      </Navbar>
    )
  }
}

In App.jsApp.js 中

You Pass the this.renderCards() as props in Navbar component.您将this.renderCards()作为Navbar组件中的道具传递。

class App extends React.Component {
  constructor(props){
    super(props);
    this.state = {currentCategory: null};
    this.renderCards = this.rendercards.bind(this);
  }
  renderCards(category){
  // here we will get the actual category as a parameter of the method.
    this.setState({currentCategory: category});
  }
  render(){
    return(
      <Navbar renderCards={this.renderCards} />
      <Card currentCategory={this.state.currentCategory} />
    )
  }
}

Summary: props are the best way to pass the data from child to parent or parent to child.总结:props 是将数据从子级传递给父级或父级传递给子级的最佳方式。

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

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