首页 文章

在Redux React Native react-navigator中传递参数

提问于
浏览
2

我试图从MenuScreen.js传递一个参数到ProfileScreen.js . 我是React Native和Redux的新手 . 使用反应导航进行导航,我可以使用按钮推送到新屏幕 .

运用

按钮onPress = {()=> dispatch(NavigationActions.navigate({routeName:'Profile',params:{user:'baz'}}))} title =“Profile”/>

我能够通过参数,但我不知道如何在配置文件屏幕上访问它 .

运用

this.props.navigation.state.params

在ProfileScreen.js中给出了一个错误

this.props.navigation

未定义 .

以下是代码,

MainScreen.js

const MainScreen = ({ dispatch }) => {

return (
<View>

  <Button
    onPress={() =>
      dispatch(NavigationActions.navigate({ routeName: 'Profile', params: { user: 'baz' } }))}
    title="Profile"
  />
</View>
);
};

MainScreen.propTypes = {
dispatch: PropTypes.func.isRequired,
};

MainScreen.navigationOptions = {
title: 'Main',
}; 

const mapStateToProps = state => ({

});

export default connect(mapStateToProps)(MainScreen);

ProfileScreen.js

const params  = this.props.navigation.state.params;

const ProfileScreen = () => (
  <View style={styles.container}>
  <Text style={styles.welcome}>
   {params}
  </Text>
  </View>
);

ProfileScreen.navigationOptions = {
 title: 'Profile',
};

export default ProfileScreen;

index.reducer.js

import { AppNavigator } from '../navigators/AppNavigator';

const initialNavState = AppNavigator.router.getStateForAction(
  AppNavigator.router.getActionForPathAndParams('Main')
);

function nav(state = initialNavState, action) {
  let nextState;
  switch (action.type) {
    default:
      nextState = AppNavigator.router.getStateForAction(action, state);
      break;
  }
  return nextState || state;
}

const AppReducer = combineReducers({
  nav,
});

export default AppReducer;

AppNavigator.js

import MainScreen from '../components/MainScreen';
import MainScreen from '../components/MainScreen';
import ProfileScreen from '../components/ProfileScreen';

export const AppNavigator = StackNavigator({
 Main: { screen: MainScreen },
 Profile: { screen: ProfileScreen },
});

const AppWithNavigationState = ({ dispatch, nav }) => (
  <AppNavigator navigation={addNavigationHelpers({ dispatch, state: 
  nav })} /> 
);

AppWithNavigationState.propTypes = {
   dispatch: PropTypes.func.isRequired,
   nav: PropTypes.object.isRequired,
};

const mapStateToProps = state => ({
 nav: state.nav,
});

export default connect(mapStateToProps)(AppWithNavigationState);

index.ios.js

class NavChecks extends React.Component {
store = createStore(AppReducer);

render() {
  return (
    <Provider store={this.store}>
      <AppWithNavigationState />
    </Provider>
    );
  }
}

AppRegistry.registerComponent('NavChecks', () => NavChecks);

export default NavChecks;

3 回答

  • 0

    尝试将道具传递给 ProfileScreen

    const ProfileScreen = (props) => (
      <View style={styles.container}>
      <Text style={styles.welcome}>
       {props.navigation.state.params.user}
      </Text>
      </View>
    );
    
  • -2

    尝试将props传递给ProfileScreen . 在我的观点中使用react-native-router-flux进行导航,这更灵活我认为你可以在导航到其他页面时将参数作为道具传递

  • 0

    我有同样的问题,我通过在反应导航中使用withNavigation解决了它 .

    import { withNavigation } from 'react-navigation';
    export default connect(mapStateToProps, mapDispatchToProps)(withNavigation(MyComponent));
    

    https://reactnavigation.org/docs/en/with-navigation.html

相关问题