javascript - 无法将登录身份验证 token 作为参数传递

标签 javascript react-native authentication react-navigation

我将登录身份验证 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/

相关文章:

javascript - 有没有办法通过java脚本函数清除html表单中的文本并选择框字段?

java - 如何在tomcat中跳过http基本身份验证?

javascript - 使用高阶组件进行身份验证时如何延迟检查 redux 存储

android - 无法向服务器发送登录请求

javascript - 何时加载浏览器框架脚本 (e10s)?

javascript - GIF(重新)压缩的Javascript概念验证

Javascript - react native 。调用 Render() 函数时,子组件(动态列表)不呈现

javascript - React Native 按钮按下以控制导入的类状态

javascript - 在控制台窗口中运行时,for 循环中的 setTimeout 第一次打印出意外的垃圾值

google-plus - 使用 React Native 实现 Google+ 登录