Reactjs 在Redux+中传递参数;反应本机+;反应导航器
我正在尝试将参数从MenuScreen.js传递到ProfileScreen.js。我是一个新的反应本机和Redux。使用react navigation进行导航,我可以用一个按钮推到新屏幕 使用 按钮onPress={()=>dispatch(导航操作.导航({routeName:'Profile',params:{user:'baz'}))} title=“Profile”/> 我可以传递参数,但我不知道如何在配置文件屏幕上访问它 使用 this.props.navigation.state.params 在ProfileScreen.js中,给出了一个错误 这是导航 没有定义 下面是代码 MainScreen.jsReactjs 在Redux+中传递参数;反应本机+;反应导航器,reactjs,react-native,redux,react-redux,react-navigation,Reactjs,React Native,Redux,React Redux,React Navigation,我正在尝试将参数从MenuScreen.js传递到ProfileScreen.js。我是一个新的反应本机和Redux。使用react navigation进行导航,我可以用一个按钮推到新屏幕 使用 按钮onPress={()=>dispatch(导航操作.导航({routeName:'Profile',params:{user:'baz'}))} title=“Profile”/> 我可以传递参数,但我不知道如何在配置文件屏幕上访问它 使用 this.props.navigation.state
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);
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;
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;
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);
class NavChecks extends React.Component {
store = createStore(AppReducer);
render() {
return (
<Provider store={this.store}>
<AppWithNavigationState />
</Provider>
);
}
}
AppRegistry.registerComponent('NavChecks', () => NavChecks);
export default NavChecks;
AppNavigator.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);
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;
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;
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);
class NavChecks extends React.Component {
store = createStore(AppReducer);
render() {
return (
<Provider store={this.store}>
<AppWithNavigationState />
</Provider>
);
}
}
AppRegistry.registerComponent('NavChecks', () => NavChecks);
export default NavChecks;
从“../components/MainScreen”导入主屏幕;
从“../components/MainScreen”导入MainScreen;
从“../components/ProfileScreen”导入ProfileScreen;
export const AppNavigator=StackNavigator({
Main:{screen:MainScreen},
配置文件:{screen:ProfileScreen},
});
const AppWithNavigationState=({dispatch,nav})=>(
);
AppWithNavigationState.propTypes={
调度:需要PropTypes.func.isRequired,
导航:需要PropTypes.object.isRequired,
};
常量mapStateToProps=状态=>({
nav:state.nav,
});
导出默认连接(MapStateTops)(AppWithNavigationState);
index.ios.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);
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;
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;
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);
class NavChecks extends React.Component {
store = createStore(AppReducer);
render() {
return (
<Provider store={this.store}>
<AppWithNavigationState />
</Provider>
);
}
}
AppRegistry.registerComponent('NavChecks', () => NavChecks);
export default NavChecks;
类导航组件{
store=createStore(Appeducer);
render(){
返回(
);
}
}
AppRegistry.registerComponent('NavChecks',()=>NavChecks);
导出默认导航检查;
尝试将道具传递到档案屏幕
:
const ProfileScreen = (props) => (
<View style={styles.container}>
<Text style={styles.welcome}>
{props.navigation.state.params.user}
</Text>
</View>
);
const ProfileScreen=(道具)=>(
{props.navigation.state.params.user}
);
尝试将道具传递到ProfileScreen。在我看来,使用react native router flux进行导航更加灵活,我认为在导航到另一个页面时可以将参数作为道具传递我也遇到了同样的问题,我通过在react导航中使用withNavigation解决了这个问题。import { withNavigation } from 'react-navigation';
export default connect(mapStateToProps, mapDispatchToProps)(withNavigation(MyComponent));
你搞定了!成功了!谢谢mradziwon!我刚开始使用React Native,如果您允许,您能提供一种与您联系的方式,以便我在再次陷入困境时可以问一些问题寻求帮助吗?再次感谢,先生!