0
我有一个拉入3个孩子的父组件。我已经使用axios设置了外部数据调用的父级状态。从父项中检索儿童组件中的数据
我现在遇到的问题是,我在子组件上使用什么语法来从父对象中获取状态数据?
var Component_1 = React.createClass({
render(){
return(
<div className="col-sm-4">
<div className="col-sm-12">
<h5>Component 1</h5>
<div className="col-sm-12">
<svg className="svg_test"></svg>
</div>
</div>
<div className="col-sm-12">
<h5>"state data goes here?"</h5>
<h6>Component 1</h6>
</div>
</div>
);
}
});
var Component_2 = React.createClass({
render(){
return(
<div className="col-sm-4">
<div className="col-sm-12">
<h5>Component 2</h5>
<div className="col-sm-12">
<svg className="svg_test"></svg>
</div>
</div>
<div className="col-sm-12">
<h5>"state data goes here?"</h5>
<h6>Component 2</h6>
</div>
</div>
);
}
});
var Component_3 = React.createClass({
render(){
return(
<div className="col-sm-4">
<div className="col-sm-12">
<h5>Component 3</h5>
<div className="col-sm-12">
<svg className="svg_test"></svg>
</div>
</div>
<div className="col-sm-12">
<h5>"state data goes here?"</h5>
<h6>Component 3</h6>
</div>
</div>
);
}
});
var Parent_Component = React.createClass({
getInitialState: function(){
return{
data: []
}
},
componentDidMount: function(){
var _this = this;
this.serverRequest = axios.get("external data call").then(function(result){
_this.setState({
data: result.data
});
})
},
componentWillUnmount: function(){
this.serverRequest.abort();
},
render() {
return (
<div className="col-sm-12">
<Component_1></Component_1>
<Component_2></Component_2>
<Component_3></Component_3>
</div>
);
}
});
ReactDOM.render(<Parent_Component />,
document.getElementById("component"))
console.log返回undefined。另外,使用反应devtools,第一个组件声明它没有道具。 –
检查,您是否在父母组件中发送道具? Component_1>,还要确保你已经在父组件上运行。 –
Andrew
我有父组件上的状态,并且我还设置了父组件内的数据。 (数据=等) –