react-native with react-navigation 和 redux [transfer state across screen]
react-native with react-navigation and redux [transfer state across screen]
我是 react-native 及其所有库的新手。
我正在尝试使用 react-redux 将我的 InputText 的内容显示到下一个屏幕,但我不确定如何正确执行此操作。文档太冗长,只是解释理论,没有任何实际意义。
我的 StackNavigator 中有 2 个屏幕,如下所示:
const StackNav = StackNavigator({
SignName: { screen: SignNameScreen},
SignBday: { screen: SignBdayScreen}
})
SignNameScreen
和 SignBdayScreen
中的每一个都只有 TextInput,我只想让用户在屏幕上输入。
class SignNameScreen extends Component {
static navigationOptions = {
title: 'Welcome',
};
handleNext() {
Alert.alert("handle button next pressed")
// navigate("SignBday")
}
handleOnPress() {
Alert.alert("handle button login pressed")
}
submitFunc = () => {
const payload = {
email: this.email,
password: this.password
}
console.log(payload)
Alert.alert("hello: " + payload.email)
}
render() {
return (
<View style={{flex: 1, flexDirection: 'column', justifyContent:
'space-around'}}>
<TextInput placeholder="first name"
onChangeText={(text) => this.firstname = text}/>
<TextInput placeholder="last name"
onChangeText={(text) => this.lastname = text}/>
<Button
title="Next"
// onPress={this.handleNext} // this doesnt work, the function cannot access props
onPress={() => {this.props.navigation.navigate("SignBday")}}
/>
</View>
);
}
}
我尝试了各种方法,例如创建 reducer
、使用 connect
导出 class 等,但一切都破坏了代码。请有人指出我正确的方向吗?
我尝试了很多 github 的示例项目,但由于各种节点问题,它们要么无法构建,要么无法运行。抱歉,javascript 也是我的弱点。
非常感谢您的帮助。
像这样导航
onPress={() => this.props.navigation.navigate('SignBday', { 'Param_Name': 'Param_Value' })}
然后像这样在另一个屏幕中获取参数
const { params } = this.props.navigation.state;
要在下一个屏幕中获取参数值,请使用
params.Param_Name
这真的取决于应用程序,如果它是一个状态较少的简单应用程序,那么您可能不需要使用 Redux。
没有 redux。
你需要像这样传递它。
this.props.navigation.navigate('NewPage',{ paramKey: paramValue })
然后像
一样在其他页面上检索它
const {params} = this.props.navigation.state;
还要确保您在该给定组件上具有导航访问权限。
使用 Redux 稍微复杂一些。
你需要一些设置和一个动作和减速器,就像完成的那样 here and here。
祝你好运!
我是 react-native 及其所有库的新手。
我正在尝试使用 react-redux 将我的 InputText 的内容显示到下一个屏幕,但我不确定如何正确执行此操作。文档太冗长,只是解释理论,没有任何实际意义。
我的 StackNavigator 中有 2 个屏幕,如下所示:
const StackNav = StackNavigator({
SignName: { screen: SignNameScreen},
SignBday: { screen: SignBdayScreen}
})
SignNameScreen
和 SignBdayScreen
中的每一个都只有 TextInput,我只想让用户在屏幕上输入。
class SignNameScreen extends Component {
static navigationOptions = {
title: 'Welcome',
};
handleNext() {
Alert.alert("handle button next pressed")
// navigate("SignBday")
}
handleOnPress() {
Alert.alert("handle button login pressed")
}
submitFunc = () => {
const payload = {
email: this.email,
password: this.password
}
console.log(payload)
Alert.alert("hello: " + payload.email)
}
render() {
return (
<View style={{flex: 1, flexDirection: 'column', justifyContent:
'space-around'}}>
<TextInput placeholder="first name"
onChangeText={(text) => this.firstname = text}/>
<TextInput placeholder="last name"
onChangeText={(text) => this.lastname = text}/>
<Button
title="Next"
// onPress={this.handleNext} // this doesnt work, the function cannot access props
onPress={() => {this.props.navigation.navigate("SignBday")}}
/>
</View>
);
}
}
我尝试了各种方法,例如创建 reducer
、使用 connect
导出 class 等,但一切都破坏了代码。请有人指出我正确的方向吗?
我尝试了很多 github 的示例项目,但由于各种节点问题,它们要么无法构建,要么无法运行。抱歉,javascript 也是我的弱点。
非常感谢您的帮助。
像这样导航
onPress={() => this.props.navigation.navigate('SignBday', { 'Param_Name': 'Param_Value' })}
然后像这样在另一个屏幕中获取参数
const { params } = this.props.navigation.state;
要在下一个屏幕中获取参数值,请使用
params.Param_Name
这真的取决于应用程序,如果它是一个状态较少的简单应用程序,那么您可能不需要使用 Redux。
没有 redux。 你需要像这样传递它。
this.props.navigation.navigate('NewPage',{ paramKey: paramValue })
然后像
一样在其他页面上检索它const {params} = this.props.navigation.state;
还要确保您在该给定组件上具有导航访问权限。
使用 Redux 稍微复杂一些。
你需要一些设置和一个动作和减速器,就像完成的那样 here and here。
祝你好运!