2017-09-13 80 views
1

我想在我的注册页面中使用终极版,所以我创建了一个用户减速器:终极版mapStateToProps连接不正确

const user = (state = initialState, action) => { 
    switch (action.type) { 
     case 'TYPE_FIRSTNAME': 
      console.log('typed first name ' + action.text); 
      return { ...state, firstName: action.text }; 
     case 'TYPE_LASTNAME': 
      return { ...state, lastName: action.text }; 
     case 'TYPE_EMAIL': 
      return { ...state, email: action.text }; 
     case 'TYPE_PASSWORD': 
      return { ...state, password: action.text }; 
     default: 
      return state; 
    } 
} 

它创建这样的:

const AppReducer = combineReducers({ 
     nav, 
     user 
    }); 
export default AppReducer; 

导航减速是对于导航(与反应导航一起使用,它工作正常)。之后,我创建了一个容器:

const mapStateToProps = state => { 
    return { 
     firstName: state.firstName, 
     lastName: state.lastName, 
    } 
}; 
const mapDispatchToProps = (dispatch, ownProps) => ({ 
    typeFirstName: (text) => {console.log('typed firstname'); 
    dispatch({type: 'TYPE_FIRSTNAME', text})}, 
    typeLastName: (text) => dispatch({type: 'TYPE_LASTNAME', text}), 
    registerUser:() => { 
     //register("mamad"); 
     console.log('called register user : ');   
     dispatch({type: 'MAINSCREEN'}) 
    } 
}); 

export default connect(mapStateToProps, 
    mapDispatchToProps)(RegisterScene) 

但它从来没有被调用,为什么?

+0

你是说如果你把一个调试器放在''mapStateToProps'中的'return {'行中,它永远不会被命中? –

+0

什么都不叫?如果你打电话给类似'typeFirstName'的动作创作者,请告诉我们你可以将代码 –

+0

添加到codesandbox.io – Deano

回答

2

我发现的唯一的问题是mapStateToProps。我认为它应该是

const mapStateToProps = state => { 
    return { 
     firstName: state.user.firstName, 
     lastName: state.user.lastName, 
    } 
}; 

如果你把错误日志放在这里会很有帮助。

2

当你组合reducer时,状态被放入指定的状态分支。

在你的情况下,你需要state.user

所以你mapStateToProps功能应该像这样:

const mapStateToProps = state => { 
    return { 
     firstName: state.user.firstName, 
     lastName: state.user.lastName, 
    } 
}; 
+0

非常感谢,它现在的作品:) – Mamadou