使用redux获取与React Navigation相关的参数

时间:2019-01-08 11:07:45

标签: react-native react-redux react-navigation

我在redux中使用反应导航。在文档中,据说不再支持redux集成。因此,建议不使用导航道具进行导航。在navigation(routeName,params)函数中,我们可以传递routeName和params(请查看NavigationSerice.js)。如何访问通过NavigationService.navigate('Home',{msg:'abcd'})传递的参数?

NavigationService.js

import { NavigationActions } from 'react-navigation';
let navigator;

function setTopLevelNavigator(navigatorRef) {
  navigator = navigatorRef;
}

function navigate(routeName, params) {
  navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params,
    })
  );
}

export default {
  navigate,
  setTopLevelNavigator,
};

AuthActions.js

export const loginChecked = (registrationNo, password) => {
  return (dispatch) => {

    const data = new FormData();
    data.append('un', registrationNo);
    data.append('pa', password);
    fetch(url, {
      method: 'POST',
       body: data
    })
    .then((response) => response.json())
    .then(responseJson => {
        NavigationService.navigate('Home', { 
            msg: responseJson.msg           //How can I access this msg params passed here
    });
     })
  };
};

Home.js

componentDidMount() {
    console.log('navigationMsg', this.props.navigation.msg); //gives undefined
    console.log('navigationMsg1', this.props.navigation.getParam('msg', 'abc')); //gives undefined
}

0 个答案:

没有答案