【发布时间】:2018-02-15 04:22:11
【问题描述】:
Im 试图将 prop 传递给另一个组件。数据(事件)来自 fetch 调用。我确实得到了道具,但不是我期望的那样。 当我控制台登录 willMount 时,我只能获得第一个对象,而当我控制台登录渲染功能时,我确实收到了两个对象。理想情况下,我想在 willMount 函数中获取这两个对象。任何人都可以解释为什么会发生这种情况,以及他们是否是一种让他们都控制台登录 willMount 的方法? Thank 你.
//PARENT
whoHasGame = (event) => {
if(event.error){
return;
}else{
console.log("event in MV", event)//console logs correctly
this.setState({
leagueInfo: <LeagueCard event={event}/>
})
}
}
render(){
return (
<ScrollView>
{this.state.leagueInfo}
</ScrollView>
);
}
//CHILD
export default class LeagueCard extends Component {
constructor(props) {
super(props);
this.state = {}
}
componentWillMount() {
console.log(this.props.event) //only the first object makes it
};
render() {
//console.log(this.props.event)<-- but here both objects make it
return (
<View>
<CardSection>
<Text>League Name Goes Here</Text>
{/* <TouchableOpacity onPress={this.display}>
<Card>
<CardSection>
{this.state.matches}
</CardSection>
</Card>
</TouchableOpacity> */}
</CardSection>
</View>
)
}
}
【问题讨论】:
-
是
componentWillMount还是componentDidMount?在阙。你说didMount。另外请分享whoHasGame如何与dom联系 -
@RIYAJKHAN 它会安装我的道歉。我还截屏了 whoHasGame 的调用方式。
-
两个对象都没有在一次渲染中获得控制台。它们正在从您的共享输出中逐一渲染。请纠正我。
componentWillMount在生命周期中仅调用一次。render为每次更新调用 -
@RIYAJKHAN 这是正确的。这似乎是我的问题。
标签: javascript ios reactjs react-native react-props