React native 将值动态传递给子组件react native
App.jsReact native 将值动态传递给子组件react native,react-native,React Native,App.js render() { return ( <View style={{ flex: 1, marginTop: 20 }}> <Button title="Learn More" color="#841584" accessibilityLabel="Learn more about this purple button" onPress={() => { (this.state.
render() {
return (
<View style={{ flex: 1, marginTop: 20 }}>
<Button
title="Learn More"
color="#841584"
accessibilityLabel="Learn more about this purple button"
onPress={() => {
(this.state.dadda = '2017-09-07');
}}
/>
<EventCalendar
eventTapped={this._eventTapped.bind(this)}
events={this.state.events}
width={width}
initDate={this.state.dadda}
scrollToFirst={false}
/>
</View>
); }
render(){
返回(
{
(this.state.dadda='2017-09-07');
}}
/>
); }
这是我的父组件,我想将initDate传递给事件日历组件,我想在按下按钮时更新日期?你不应该通过直接给
状态变量赋值来改变状态,而应该使用设置状态来实现这一点
<Button
title="Learn More"
color="#841584"
accessibilityLabel="Learn more about this purple button"
onPress={() => this.setState({dadda: '2017-09-07'})}
/>
this.setState({dadda:'2017-09-07'})
/>
将dadaaa
设置为状态的方式不正确,请使用此功能。改为使用setState
功能,请查看
将onPress处理程序更改为
...
< Button
title = "Learn More"
color = "#841584"
accessibilityLabel = "Learn more about this purple button"
onPress = {
() => {
this.setState({
dadda:'2017-09-07'
});
}
}
/>
...
。。。
<按钮
title=“了解更多信息”
color=“#841584”
accessibilityLabel=“了解有关此紫色按钮的详细信息”
onPress={
() => {
这是我的国家({
达达:“2017-09-07”
});
}
}
/>
...
另外,不要在render中声明函数,而是将其保持在类级别,如
onPressHandler = () => {
this.setState({
dadaa: 'aaaa'
});
}
render() {
return (
...
<
Button
...
onPress = {this.onPressHandler}
...
/>
...
);
}
onPressHandler=()=>{
这是我的国家({
达达:“aaaa”
});
}
render(){
返回(
...
<
按钮
...
onPress={this.onPressHandler}
...
/>
...
);
}
希望这会有帮助