我试图使用react-native-router-flux将数据从一个屏幕发送到另一个屏幕,但问题是我无法在屏幕的接收端接收数据。这是我的代码片段:
在router.js中
<Router>
<Stack key="root">
<Scene
key="signup"
component={Signup}
title=""
initial={true}
hideNavBar={true}
/>
<Scene key="login" component={Login} title="" />
<Scene key="bottomtourbar" tabs={true} hideNavBar={true}>
<Scene
key="toursHome"
component={Tours}
title="Tours"
hideTabBar={true}
hideNavBar={false}
/>
<Scene
key="analytics"
component={Analytics}
title="Analytics"
hideNavBar={true}
hideNavBar={false}
/>
</Scene>
</Stack>
</Router>
在Login.js中我使用下面的代码将参数传递给toursHome页面
Actions.toursHome({dataResponse:jsonResponse});
在ToursHome.js文件中,我试图访问登录页面发送的参数
export default class Tours extends Component {
constructor(props) {
super(props);
};
render() {
const responseData = this.props.dataResponse;
console.log(responseData);
return (
<View style={styles.container}>
<ToursTab />
<View style={styles.TabbedContainer}>
<Text>{responseData}</Text>
</View>
<ToursBottom />
</View>
);
}
}
在控制台中,它打印为未定义。
通过使用“Actions.login({dataResponse:jsonResponse});”,从注册屏幕发送道具到登录工作,但是从登录到标签栏屏幕发送道具失败。任何人都可以帮我解决这个问题。
我试过以很多不同的方式实现这一目标。但我学会了通过Actions
将道具传递到不同层次的场景(深度,如你的情况下toursHome
场景嵌套在bottomtourbar
场景下)的“艰难方法”只能通过redux正确完成。如果你的场景连接到redux(使用connect()),那么更新商店中的状态,它应该通过mapStateToProps
可用于你连接的组件。
否则,使用Actions
在场景之间传递道具对于相同级别/深度的场景是可行的(例如在你的情况下,它将成功传递Actions
和analytics
或toursHome
和signup
之间的login
的道具)