I have (e.g.) two components in React. The first, app.js
, is the root component. It imports some JSON
data and puts it in its state
. This works fine (I can see it in the 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>
)
}
}