Redux 状态在函数上未定义但在 render() 上未定义
Redux state undefined on function but not on render()
我正在使用 redux,我想根据用户是否登录来呈现我的主抽屉。我有这个 'init' class 会检查并相应地做。
我会post代码并在最后解释它:
Init.js
const mapStateToProps = (userReducer) => {
return {
...userReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
getCurrentUser: () => {
dispatch(getCurrentUser())
}
}
}
class Init extends Component {
constructor(props) {
super(props);
this.props.getCurrentUser()
console.log("TEST>>")
console.log(this.props)
}
chooseInitialRoute() {
const { navigation, user } = this.props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}
user_actions.js
export function getCurrentUser() {
return (dispatch) => {
Parse.User.currentAsync().then(function (user) {
if (user) {
dispatch({ type: 'GET_USER', payload: { user } })
} else {
dispatch({ type: 'GET_USER_REJECTED', payload: error })
}
})
}
}
console.log(this.props) returns undefined 如果在构造函数中调用(当我做 console.log(props)。但如果我在 render() 中调用它,我会正确地获得道具(这意味着我从解析服务器获得正确的用户对象)。
有什么办法可以在render()方法之前更新道具吗?我想在 chooseInitialRoute()
上获得 'updated' 道具
提前致谢。如果需要更多代码,请告诉我。
如果 console.log(props)
在构造函数中未定义,这意味着组件稍后通过 componentWillReceiveProps 获取道具,我不知道你在哪里调用 InitialRoute 但你可以考虑将它放在生命周期函数中:
componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}
请注意,如果您的道具在构造函数中未定义,那么在您的组件最终通过 componentWillReceiveProps 接收道具之前,您的组件可能会渲染多次。
我正在使用 redux,我想根据用户是否登录来呈现我的主抽屉。我有这个 'init' class 会检查并相应地做。
我会post代码并在最后解释它:
Init.js
const mapStateToProps = (userReducer) => {
return {
...userReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
getCurrentUser: () => {
dispatch(getCurrentUser())
}
}
}
class Init extends Component {
constructor(props) {
super(props);
this.props.getCurrentUser()
console.log("TEST>>")
console.log(this.props)
}
chooseInitialRoute() {
const { navigation, user } = this.props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}
user_actions.js
export function getCurrentUser() {
return (dispatch) => {
Parse.User.currentAsync().then(function (user) {
if (user) {
dispatch({ type: 'GET_USER', payload: { user } })
} else {
dispatch({ type: 'GET_USER_REJECTED', payload: error })
}
})
}
}
console.log(this.props) returns undefined 如果在构造函数中调用(当我做 console.log(props)。但如果我在 render() 中调用它,我会正确地获得道具(这意味着我从解析服务器获得正确的用户对象)。
有什么办法可以在render()方法之前更新道具吗?我想在 chooseInitialRoute()
上获得 'updated' 道具提前致谢。如果需要更多代码,请告诉我。
如果 console.log(props)
在构造函数中未定义,这意味着组件稍后通过 componentWillReceiveProps 获取道具,我不知道你在哪里调用 InitialRoute 但你可以考虑将它放在生命周期函数中:
componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}
请注意,如果您的道具在构造函数中未定义,那么在您的组件最终通过 componentWillReceiveProps 接收道具之前,您的组件可能会渲染多次。