在 React 中访问子节点的父状态
Accessing parent state in child in React
我在 React 中有(例如)两个组件。第一个 app.js
是根组件。它导入一些 JSON
数据并将其放入其 state
中。这工作正常(我可以在 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>
);
}
}
第二个 docs.js
旨在显示此 JSON
数据。为此,它需要访问 app.js
的 state
。目前它出错了,我知道为什么(this
不包括app.js
)。但是我怎样才能将 state
从 app.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>
)
}
}
正确的做法是将状态作为 props
传递给 Docs
组件。
但是,因为您使用的是 React Router
,所以可以通过稍微不同的方式访问它:this.props.route.param
而不是默认的 this.props.param
因此您的代码应该大致如下所示:
<Route path="/docs" component={Docs} docs={this.state.docs} />
和
{this.props.route.docs.map(function(study, key) {
return <p>Random text here</p>;
})}
另一种方法是:
<Route path="/docs" component={() => <Docs docs={this.state.docs}/>}>
如果需要通过children:
<Route path="/" component={(props) => <Docs docs={this.state.docs}>{props.children}</Docs>}>
如果您是这样做的,那么您可以通过 this.props.docs
在 Child 组件中直接访问您的道具值:
{
this.props.docs.map((study, key)=> {
return <p key={key}>Random text here</p>;
})
}
另一种方法是
<Route path='/' render={ routeProps => <Home
{...routeProps}
docs={this.state.docs}
/>
}
/>
在子组件中您可以使用
访问docs
this.props.docs
希望对您有所帮助!
我在 React 中有(例如)两个组件。第一个 app.js
是根组件。它导入一些 JSON
数据并将其放入其 state
中。这工作正常(我可以在 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>
);
}
}
第二个 docs.js
旨在显示此 JSON
数据。为此,它需要访问 app.js
的 state
。目前它出错了,我知道为什么(this
不包括app.js
)。但是我怎样才能将 state
从 app.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>
)
}
}
正确的做法是将状态作为 props
传递给 Docs
组件。
但是,因为您使用的是 React Router
,所以可以通过稍微不同的方式访问它:this.props.route.param
而不是默认的 this.props.param
因此您的代码应该大致如下所示:
<Route path="/docs" component={Docs} docs={this.state.docs} />
和
{this.props.route.docs.map(function(study, key) {
return <p>Random text here</p>;
})}
另一种方法是:
<Route path="/docs" component={() => <Docs docs={this.state.docs}/>}>
如果需要通过children:
<Route path="/" component={(props) => <Docs docs={this.state.docs}>{props.children}</Docs>}>
如果您是这样做的,那么您可以通过 this.props.docs
在 Child 组件中直接访问您的道具值:
{
this.props.docs.map((study, key)=> {
return <p key={key}>Random text here</p>;
})
}
另一种方法是
<Route path='/' render={ routeProps => <Home
{...routeProps}
docs={this.state.docs}
/>
}
/>
在子组件中您可以使用
访问docs
this.props.docs
希望对您有所帮助!