Redux mapStateToProps未正确连接

时间:2017-09-13 16:44:05

标签: reactjs redux react-redux

我想在注册页面中使用Redux,因此我创建了一个用户缩减器:

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;
导航缩减器用于导航(与react-navigation一起使用,它可以正常工作)。之后我创建了一个容器:

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)

但它永远不会被称为,为什么?

2 个答案:

答案 0 :(得分:2)

组合reducer时,状态会进入指定的状态分支。

在您的情况下,您需要scope :shortlived, -> { where('updated_at < created_at + interval 3 day') }

所以你的mapStateToProps函数应如下所示:

state.user

答案 1 :(得分:2)

我找到的唯一问题是mapStateToProps。我认为应该是

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

如果您将错误日志放在此处会很有帮助。