我将登录身份验证 token 存储在一个状态中,并将其作为参数传递到导航上的另一个屏幕。但是接收该 token 作为参数的屏幕在 console.log() 上显示“未定义”。我不想每次在每个屏幕上都从异步存储中获取 token ,而是想使用参数传递方法。
主要组件从父类接收 token 作为 Prop ,并进一步将其通过导航发送到另一个子屏幕。
class DropdownSales extends Component {
constructor(props)
{
super(props)
this.state = {
id: this.props.auth,
TotalSales: [],
data_default:{},
}
}
componentDidMount(){
this.fetch_default();
}
setPickerValue = (newValue) =>
{
this.props.navigation.navigate('OemLevelTwo', {value: newValue}, {auth_token: this.state.id});
this.setState({
pickerSelection: newValue,
});
this.togglePicker();
...........
...........
}
}
这是需要使用token参数的组件。
export default class OemLevelTwo extends Component {
constructor(props) {
super(props)
this.state = {
id: '',
param_oem: '',
}
}
async componentDidMount(){
const param_oem = this.props.navigation.getParam('value');
const auth_id = this.props.navigation.getParam('auth_token');
await this.setState({
param_oem,
id: auth_id
});
console.log(this.state.id, 'in oem level two')
this.fetch_oem();
}
fetch_oem = () => {
var headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'x-access-token',
'x-access-token': this.state.id
}
.............
.............
}
}
当我从“OemLevelTwo”进行控制台时,它会打印 undefined
作为输出。
请帮忙解决。
最佳答案
.navigate()
的第二个参数可用于传递参数。在您的情况下,您将 {value: newValue}
作为第二个参数传递,并将 {auth_token: this.state.id}
作为第三个参数传递。
因此将您的行更改为:
this.props.navigation.navigate('OemLevelTwo', {value: newValue, auth_token: this.state.id});
现在您应该能够使用 getParam('auth_token');
关于javascript - 无法将登录身份验证 token 作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036267/