cg1*_*991 4 react-native react-navigation
我仍然是新的反应导航.我想在登录成功后重置路由信息.但是有一些参数,比如我希望传递给主页面的登录信息.
import { NavigationActions } from 'react-navigation'
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Profile',
params:{user:this.state.username}})
]
})
this.props.navigation.dispatch(resetAction)
Run Code Online (Sandbox Code Playgroud)
在Profile页面中,我想访问params,所以我使用它:
constructor(props){
super(props)
this.state = { username: this.props.navigation.state.params.user,
}
Run Code Online (Sandbox Code Playgroud)
但我收到:undefined不是控制台日志中的对象(评估'_this.props.navigation.state.params.user').我可以知道如何获取参数?谢谢
这对我在 React Navigation 5 上有用:
要重置导航状态,请导航到“OrderDetails”屏幕并将参数“orderCode”传递到此屏幕:
navigation.reset({index: 0, routes: [{ name: 'OrderDetails', params: { orderCode: 'whatever' } }] })
Run Code Online (Sandbox Code Playgroud)
要在新路由“OrderDetails”中检索参数 orderCode:
function OrderDetails({theme, route, navigation}) {
const { orderCode } = route.params;
...
}
Run Code Online (Sandbox Code Playgroud)
您可以在react-native stack导航器的reset操作中传递params,如下所示: -
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'ScreenName', // name of the screen you want to navigate
params: {
paramsKey: paramsValue // this second parameter is for sending the params
}
})
],
});
this.props.navigation.dispatch(resetAction);
Run Code Online (Sandbox Code Playgroud)
并从导航状态参数获取第二个屏幕中的参数值,如下所示: -
static navigationOptions = ({navigation, screenProps}) => {
const params = navigation.state.params || {}; // You can get the params here as navigation.state.params.paramsKey
console.log("CompareScreen product_id:", params.paramsKey);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
是的,它可以。React Navigation 5+ 让我们轻松使用导航道具重置路线历史记录,然后它允许我们将params属性传递给目标路线对象。
navigation.reset({
index: 0,
routes: [{name: 'DriveInfosAdded', params: {data}}],
});
Run Code Online (Sandbox Code Playgroud)
您正在使用usernameas 键传递参数。因此,当您检索它时,您的代码应该如下所示
第1步:传递参数
this.props.navigation.navigate('Your-Screen_name',{ username: 'Lucy' })
Run Code Online (Sandbox Code Playgroud)
Step2:获取参数
this.state = {
username: this.props.navigation.state.params.username,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3051 次 |
| 最近记录: |