无法访问路由参数(功能组件)
Unable to access route params (functional component)
我正在尝试将用户名从 signup1 传递给 signup 2:
handleSignUp = () => {
console.log(this.state.username) <------------- This logs correctly
try {
this.props.navigation.navigate('Signup2', {
username: this.state.username, <------------- Passing here
});
} catch (error) {
console.log(error);
}
}
这是来自 Signup2 的代码:
const Signup2 = (props, route, navigation) => {
const { username } = route.params; <----------- Trying to access it here
我收到以下错误:
undefined is not an object (evaluating 'route.params.username')
我也试过这个:
const { username } = props.route.params
而且都不起作用。
如何访问路由参数?
我相信如果您想按预期访问它们,您需要解构路由和导航属性。
你可以去:
const Signup2 = ({route, navigation}) => {
const { username } = route.params
}
或者也许:
const Signup2 = (props) => {
const { username } = props.route.params
}
我正在尝试将用户名从 signup1 传递给 signup 2:
handleSignUp = () => {
console.log(this.state.username) <------------- This logs correctly
try {
this.props.navigation.navigate('Signup2', {
username: this.state.username, <------------- Passing here
});
} catch (error) {
console.log(error);
}
}
这是来自 Signup2 的代码:
const Signup2 = (props, route, navigation) => {
const { username } = route.params; <----------- Trying to access it here
我收到以下错误:
undefined is not an object (evaluating 'route.params.username')
我也试过这个:
const { username } = props.route.params
而且都不起作用。
如何访问路由参数?
我相信如果您想按预期访问它们,您需要解构路由和导航属性。
你可以去:
const Signup2 = ({route, navigation}) => {
const { username } = route.params
}
或者也许:
const Signup2 = (props) => {
const { username } = props.route.params
}