将 `store` 设置为 reducer 的 `state` 参数

Set `store` as reducer's `state` parameter

我是 Redux(还有 React)的新手,所以这可能是一个非常基本的问题,但我无法在 Internet 上找到直接的答案。

我的应用程序上有一个注册屏幕,我称之为 SignUp。它非常简单,只有三个输入(emailpasswordpasswordCheck)和一个按钮(signup)。 事情是,我想让我的用户生活尽可能简单,所以当 passwordCheck 发生变化时,我会对照 password 进行检查。如果它们匹配,signup 将被设置为启用,如果它们不匹配,signup 将被禁用并向用户显示一条消息。 当我只使用 React 时,事情非常简单——我只是做了一个 this.state:

this.state: {
    // ... stuff
    password: "",
    passwordIsValid: false, //test password against a predicate function
    passwordMessage: "", //if the password is not valid there is a message
    passwordStyle: "", //if password is wrong i put some classes here
    passwordCheck: "",
    passwordCheckMessage: "", //the 'passwords do not match' message goes here
    passwordCheckStyle: "",
    passwordsMatch: false
}

然后我在 SignUp 中处理了应用程序的状态。 现在我也在使用 Redux,所以我终止了 this.state,将所有内容移至 store 并开始使用 reducer 而不是处理程序。 password 一切正常,但 passwordCheck 是另一回事。因为我需要知道 Store 的 password 来对照 passwordCheck 检查它,所以我无法(到目前为止?)在 passwordCheckReducer 上进行检查。相反,我从 passwordCheckReducer 中删除了 passwordCheckMessagepasswordCheckStylepasswordsMatch,并在 SignUp 上计算了这些值。不过,在我看来,这是解决整个问题的一种非常错误和丑陋的方式。

所以,相反,我想要一个更优雅、更像 Redux 的解决方案。

如果我能让 store 出现在 passwordCheckReducerstate 上,我将能够在 reducer 中设置 passwordsMatch 和其他的同时保持它的纯净.不幸的是,我一直无法做到(到目前为止?)。 所以,我想知道我想做的事情是否可行,或者是否有其他更理想的方法。

OBS1:在网上随便找找,发现Redux官方文档关于初始化状态1。不过,我不认为 preloadedState 是解决我问题的方法,因为它用于初始化 store,而不是让它在我的减速器上可用。相反,我只需要让 store - 即使是空的 - 对 passwordCheckReducer 可见。 OBS2:我知道我可以将 store 作为 action 的键来传递,但是由于 reducer 模式包含一个 state 参数,因此在 action 中定义这样的传递对我来说似乎是多余的.

这是我的店铺:

// store.js
import { applyMiddleware, createStore } from 'redux';
import { createLogger } from 'redux-logger';
import thunkMiddleare from 'redux-thunk';
import { Reducers } from '../reducers/reducers';

const logger = createLogger();
const middleware = applyMiddleware(thunkMiddleare, logger);

export const Store = createStore(Reducers, middleware);

我正在使用 combineReducers:

// reducers.js
import { combineReducers } from 'redux';
import { emailReducer } from './emailReducer';
import { passwordReducer } from './passwordReducer';
import { passwordCheckReducer } from './passwordCheckReducer';

export const Reducers = combineReducers({
    emailChange: emailReducer,
    passwordChange: passwordReducer,
    passwordCheckChange: passwordCheckReducer
}); 

这里是 passwordCheckReducer

// passwordCheckReducer.js
import { CHANGE_PASSWORDCHECK } from '../actions/actionTypes';

const initialState = {
    passwordCheck: ""
};

export const passwordCheckReducer = (state=initialState, action) => {
    const { payload, type } = action;

    switch(type) {
        case CHANGE_PASSWORDCHECK:
            const passwordCheck = payload;
            return {
                ...state,
                passwordCheck
            };
        default: 
            return state;
    }
};

最后,这是mapDispatchToProps的实现:

// SignUp.js
const mapDispatchToProps = dispatch => ({
    handleEmailChange: e => dispatch(updateEmail(e.target.value)),
    handlePasswordChange: e => dispatch(updatePassword(e.target.value)),
    handlePasswordCheckChange: e => dispatch(updatePasswordCheck(e.target.value))
});

如果我没记错的话,当您将 reducer 传递给 combineReducers 时,该 reducer 将接收 redux 模块状态(因此,initialState 是什么,而不是整个应用程序的根状态对象)。您无权访问其他减速器的状态。

你有几个选择。

我认为 passwordpasswordCheck 有单独的减速器有点矫枉过正。我会为整个表格使用一个减速器:

const initialState = {
    password: '',
    passwordCheck: '',
    // ....
}

export const signupReducer = (state=initialState, action) => {
    const { payload, type } = action;

    switch(type) {
        case CHANGE_PASSWORD:
            return { ...state, password: action.password}
        case CHANGE_PASSWORDCHECK:
            const passwordCheck = payload;
            if (state.password != state.passwordCheck) 
                return { ...state, passwordCheck, error: "..."}; // or something like this

            return {
                ...state,
                passwordCheck
            };
        default: 
            return state;
    }
};

如果你想把事情分开,你总是可以定义化简器,并从父化简器调用它们,传递整个父化简器状态。类似于:

import passwordReducer from './passwordReducer'
import passwordCheckReducer form './passwordCheckReducer'

export const signupReducer(state = initialState, action) => {
    state = passwordReducer(state, action)
    state = passwordCheckReducer(state, action)
    return state
}

如果您正在组合 reducer,则只有一个存储对象存储来自每个 reducer 的所有状态。

因此您可以像这样从一个组件访问多个 reducer:

const mapStateToProps = state => ({
  password: state.passwordChange.password,
  passwordCheck: state.passwordCheckChange.passwordCheck,
  passwordsMatch: state.passwordCheckChange.passwordsMatch
})
export default connect(mapStateToProps, mapDispatchToProps)(SignUp)

每次更新 passwordCheckpassword 时,组件的 props 都会更新。

所以最好的解决方案实际上是将 passwordsMatch 处理为从两个单独的 reducer 的状态派生的自定义道具。类似于:

const mapStateToProps = state => {
  const passwordsMatch = state.passwordChange.password === state.passwordCheckChange.passwordCheck
  return {
    password: state.passwordChange.password, 
    passwordCheck: state.passwordCheckChange.passwordCheck,
    passwordsMatch: passwordsMatch
  }
}