子供は親の小道具にどのようにアクセスできますか?例えば、ReactJS - 子供が親の小道具にどのようにアクセスするのか?
親コンポーネント - footer.jsx:
import React from 'react';
import $ from 'jquery';
import NavItem from './nav-item';
class Footer extends React.Component
{
constructor(props) {
super(props);
this.state = {
publicUrl: null,
currentUrl: null,
navitems: [],
};
}
// Then fetch the data using $.getJSON():
componentDidMount() {
this.serverRequest = $.getJSON(this.props.source, function (result) {
this.setState({
currentUrl: window.location.href,
publicUrl: result.publicUrl,
navitems: result.nav
});
}.bind(this));
}
componentWillUnmount() {
this.serverRequest.abort();
}
render() {
var loop = this.state.navitems.map(function(item, index){
return <NavItem key={index} item={item}></NavItem>;
});
return (
<div>
<div className="nav">
<ul>{ loop }</ul>
<p>{this.state.publicUrl}</p>
<p>{this.state.currentUrl}</p>
</div>
</div>
)
}
}
export { Footer as default }
子コンポーネントは - NAV-item.jsx:
インポート 'が反応する' と反応させ; footer.jsx
(親)で
class NavItem extends React.Component
{
constructor(props) {
super(props);
}
render() {
return <li><a href={this.props.publicUrl + '/' + this.props.item.url}>{this.props.item.title}</a></li>
}
}
export { NavItem as default }
結果:nav-item.jsx
で
this.props.publicUrl // http://my-website.com
結果(子):
this.props.publicUrl // undefined but it should be http://my-website.com
任意のアイデア?
サンプルデータ:
{
"publicUrl": "http:\/\/my-website.com",
"nav": [{
"navId": "3",
"title": "Home
"code": "home
"href": null,
"style": null,
"sort": "3",
"url": "home
"parentId": null,
"totalChildren": "0",
"createdOn": null,
"updatedOn": null
}, {
"navId": "4",
"title": "About
"code": "about
"href": null,
"style": null,
"sort": "4",
"url": "about
"parentId": null,
"totalChildren": "0",
"createdOn": null,
"updatedOn": null
}, {
"navId": "5",
"title": "Contact",
"code": "contact",
"href": "#contact",
"style": null,
"sort": "5",
"url": "contact",
"parentId": null,
"totalChildren": "0",
"createdOn": null,
"updatedOn": null
}]
}
あなたは 'のように小道具を経由して、それを渡す必要があります' –
naortor