简体   繁体   English

在React中访问子节点中的父节点状态

[英]Accessing parent state in child in React

I have (eg) two components in React. 我在React中有(例如)两个组件。 The first, app.js , is the root component. 第一个是app.js ,它是根组件。 It imports some JSON data and puts it in its state . 它导入一些JSON数据并将其置于其state This works fine (I can see it in the React devtools). 这工作正常(我可以在React devtools中看到它)。

import data from '../data/docs.json';

class App extends Component {
  constructor() {
    super();
    this.state = {
      docs: {}
    };
  }
  componentWillMount() {
    this.setState({
      docs: data
    });
  }
  render() {
    return (
      <Router history={hashHistory}>
        <Route path="/" component={Wrapper}>
          <IndexRoute component={Home} />
          <Route path="/home" component={Home} />
          <Route path="/docs" component={Docs} />
        </Route>
      </Router>
    );
  }
}

The second, docs.js , is meant to show this JSON data. 第二个是docs.js ,用于显示此JSON数据。 To do that it needs to access the state of app.js . 要做到这一点,它需要访问app.jsstate At the moment it errors, and I know why ( this does not include app.js ). 目前它出错了,我知道为什么( this不包括app.js )。 But how then can I pass the state from app.js to docs.js ? 但是,如何将stateapp.js传递给docs.js

class Docs extends React.Component {
    render() {
        return(
            <div>
                {this.state.docs.map(function(study, key) { 
                    return <p>Random text here</p>; 
                })}
            </div>
        )
    }
}

The proper way of doing this would be by passing state as props to Docs component. 这样做的正确方法是将状态作为props传递给Docs组件。

However, because you are using React Router it can be accessed in a bit different way: this.props.route.param instead of default this.props.param 但是,因为您使用的是React Router所以可以通过不同的方式访问它: this.props.route.param而不是默认的this.props.param

So your code should look more or less like this: 所以你的代码应该或多或少看起来像这样:

<Route path="/docs" component={Docs} docs={this.state.docs} />

and

{this.props.route.docs.map(function(study, key) { 
    return <p>Random text here</p>; 
})}

Another way of doing this is: 另一种方法是:

<Route path="/docs" component={() => <Docs docs={this.state.docs}/>}>

If you need to pass children: 如果你需要传递孩子:

<Route path="/" component={(props) => <Docs docs={this.state.docs}>{props.children}</Docs>}>

If you are doing it like this, then you can access your props values directly by this.props.docs in Child Component: 如果您这样做,那么您可以直接通过子组件中的this.props.docs访问您的道具值:

{
    this.props.docs.map((study, key)=> { 
       return <p key={key}>Random text here</p>; 
    })
}

Another way of doing this will be 另一种方法是这样做

        <Route path='/' render={ routeProps => <Home 
            {...routeProps} 
            docs={this.state.docs}
            /> 
          } 
        />

While in the child component you can access docs using 而在子组件,您可以访问docs使用

this.props.docs

Hope it helps! 希望能帮助到你!

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

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