Redux状态道具值未定义

时间:2017-11-29 07:14:38

标签: reactjs redux redux-thunk

嘿大家所以我正在创建用户登录的模拟工作。我正在使用redux logger,我记录的状态值总是我想看的值,但是当我是console.log时( this.props)

我总是回复未定义。我的reducer有一个我定义的状态值,并作为默认值传入。我不确定为什么我会回复未定义但只是无法弄明白。这是我的代码。任何帮助将非常感谢我需要访问这些值,但未定义它不会有效。

我将压缩锅炉板的东西,如import语句等。也是为了上下文。我想要做的就是在我的组件内部能够访问我的reducer中定义的状态值。例如,其中一个是isLoginPending。当我是console.logging this.props.isLoginPending时,我希望得到默认值或新的Object.assigned值而不是未定义。这是我理想的目标是获取未定义的组件内部的值。

这是我的组件

render() {
        let {email, password} = this.state;
        console.log("PROPS*****" + this.props.isLoginPending);
        return (
            <div className="form-wrapper" >
                <form onSubmit={this.submit} name="login-form">
                    <label htmlFor="login-form">Email</label>
                    <input onChange={this.changedEmail} type="email" />
                    <br />
                    <label htmlFor="login-form"> Password </label>
                    <input onChange={this.changedPassword} type="password" />
                    <button type="submit">Login </button>

                </form>
            </div>
        )
    }
}

const mapStateToProps = (state) => {
    return {
       isLoginPending: state.isLoginPending,
       isLoginSuccess: state.isLoginSuccess,
       isloginError: state.isloginError
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        login: (email, password) => dispatch(login(email ,password))
    };
}


export default connect(mapStateToProps, mapDispatchToProps)(LoginForm);

Reducer

 export default function (state = {
        isLoginPending: false,
        isLoginSuccess: false,
        isLoginError: null
    }, action)
     {
        switch(action.type) {
            case constants.LOGIN_SUCCESS:
            console.log("state" + state.isLoginPending);
            return Object.assign({}, state, {isLoginSuccess: action.isLoginSuccess})

            case constants.LOGIN_PENDING:
            return Object.assign({}, state, {isLoginPending: action.isLoginPending})

            case constants.LOGIN_ERROR:

            return Object.assign({}, state, {isLoginError: action.isLoginError})

            default: 
            return state
        }
    }

操作

export const login = (email, password) => {
    return dispatch => {
        dispatch(loginPending(true));
        dispatch(loginSuccess(false));
        dispatch(loginError(null));

    sendLoginRequest(email, password, error => {
        dispatch(loginPending(false));
        if(!error) {
            dispatch(loginSuccess(true));
        } else {
            dispatch(loginError(error));
        }
    });
    }
}

const sendLoginRequest = (email, password, callback) => {
    setTimeout(() => {
        if(email === 'admin@admin.com' && password === 'password') {
            return callback(null);
        } 

        else {
            return callback(new Error("invalid email or password"));
        }
    }, 1000)
}

**编辑**

const createStoreWithMiddleware = applyMiddleware(thunk, logger)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <App />
  </Provider>

传递到商店的根reducer

const rootReducer = combineReducers({
  loginForm: emailReducer
});

export default rootReducer;

2 个答案:

答案 0 :(得分:3)

问题是你的mapStateToProps对象,即你希望标志位于rootstate上

const mapStateToProps = (state) => {
    return {
       isLoginPending: state.isLoginPending,
       isLoginSuccess: state.isLoginSuccess,
       isloginError: state.isloginError
    }
}

但是,当我们看到你的root reducer时,你有一个多级状态,在state.loginForm

中添加这些标志
const rootReducer = combineReducers({
  loginForm: emailReducer
});

所以改变你的mapStateToProps就应该这样做

const mapStateToProps = (state) => {
    return {
       isLoginPending: state.loginForm.isLoginPending,
       isLoginSuccess: state.loginForm.isLoginSuccess,
       isloginError: state.loginForm.isloginError
    }
}

答案 1 :(得分:0)

您通过loginFormrootReducer作为商店传递给关联的组件:

const rootReducer = combineReducers({
  loginForm: emailReducer
});  

所以你可以在mapstateToProps里面做:

const mapStateToProps = (state) => {
    return {
       isLoginPending: state.loginForm.isLoginPending,
       isLoginSuccess: state.loginForm.isLoginSuccess,
       isloginError: state.loginForm.isloginError
    }
}