我不知道如何从isLoading
中的reducerForm.js
reducer访问布尔reducerRegister.js
标记。我使用combineReducers()
并使用isLoading
在表单提交期间禁用了按钮。
初始状态为false,点击提交后,它会更改为true
。表单提交成功后,isLoading
将再次重置为false
。以下是此问题的相关代码:
actionRegister.js
let _registerUserFailure = (payload) => {
return {
type: types.SAVE_USER_FAILURE,
payload
};
};
let _registerUserSuccess = (payload) => {
return {
type: types.SAVE_USER_SUCCESS,
payload,
is_Active: 0,
isLoading:true
};
};
let _hideNotification = (payload) => {
return {
type: types.HIDE_NOTIFICATION,
payload: ''
};
};
// asynchronous helpers
export function registerUser({ // use redux-thunk for asynchronous dispatch
timezone,
password,
passwordConfirmation,
email,
name
}) {
return dispatch => {
axios.all([axios.post('/auth/signup', {
timezone,
password,
passwordConfirmation,
email,
name,
is_Active: 0
})
// axios.post('/send', {email})
])
.then(axios.spread(res => {
dispatch(_registerUserSuccess(res.data.message));
dispatch(formReset());
setTimeout(() => {
dispatch(_hideNotification(res.data.message));
}, 10000);
}))
.catch(res => {
// BE validation and passport error message
dispatch(_registerUserFailure(res.data.message));
setTimeout(() => {
dispatch(_hideNotification(res.data.message));
}, 10000);
});
};
}
actionForm.js
export function formUpdate(name, value) {
return {
type: types.FORM_UPDATE_VALUE,
name, //shorthand from name:name introduced in ES2016
value
};
}
export function formReset() {
return {
type: types.FORM_RESET
};
}
reducerRegister.js
const INITIAL_STATE = {
error:{},
is_Active:false,
isLoading:false
};
const reducerSignup = (state = INITIAL_STATE , action) => {
switch(action.type) {
case types.SAVE_USER_SUCCESS:
return { ...state, is_Active:false, isLoading: true, error: { register: action.payload }};
case types.SAVE_USER_FAILURE:
return { ...state, error: { register: action.payload }};
case types.HIDE_NOTIFICATION:
return { ...state , error:{} };
}
return state;
};
export default reducerSignup;
reducerForm.js
const INITIAL_STATE = {
values: {}
};
const reducerUpdate = (state = INITIAL_STATE, action) => {
switch (action.type) {
case types.FORM_UPDATE_VALUE:
return Object.assign({}, state, {
values: Object.assign({}, state.values, {
[action.name]: action.value,
})
});
case types.FORM_RESET:
return INITIAL_STATE;
// here I need isLoading value from reducerRegister.js
}
return state;
};
export default reducerUpdate;
reducerCombined.js
import { combineReducers } from 'redux';
import reducerRegister from './reducerRegister';
import reducerLogin from './reducerLogin';
import reducerForm from './reducerForm';
const rootReducer = combineReducers({
signup:reducerRegister,
signin: reducerLogin,
form: reducerForm
});
export default rootReducer;
这是我使用isLoading
:
let isLoading = this.props.isLoading;
<FormGroup>
<Col smOffset={4} sm={8}>
<Button type="submit" disabled={isLoading}
onClick={!isLoading ? isLoading : null}
>
{ isLoading ? 'Creating...' : 'Create New Account'}
</Button>
</Col>
</FormGroup>
将状态映射到同一组件中的道具
function mapStateToProps(state) {
return {
errorMessage: state.signup.error,
isLoading: state.signup.isLoading,
values: state.form.values
};
}
答案 0 :(得分:20)
这在http://redux.js.org/docs/faq/Reducers.html#reducers-share-state的Redux常见问题解答中有所介绍:
许多用户后来想尝试在两个reducer之间共享数据,但发现combineReducers不允许他们这样做。有几种方法可以使用:
- 如果reducer需要知道来自另一个状态片的数据,则可能需要重新组织状态树形状,以便单个reducer处理更多数据。
- 您可能需要编写一些自定义函数来处理其中一些操作。这可能需要将combineReducers替换为您自己的顶级reducer功能。您还可以使用reduce-reducers等实用程序来运行combineReducers来处理大多数操作,还可以为跨越状态切片的特定操作运行更专业的reducer。
- 异步动作创建者(如redux-thunk)可以通过getState()访问整个状态。操作创建者可以从状态中检索其他数据并将其放入操作中,以便每个reducer都有足够的信息来更新自己的状态切片。
答案 1 :(得分:12)
reducer无法访问另一个reducer的状态,但如果您使用redux-thunk,则可以在动作创建者中执行此操作。例如,您可以定义这样的动作创建者:
export const someAction = () =>
(dispatch, getState) => {
const someVal = getState().someReducer.someVal;
dispatch({ type: types.SOME_ACTION, valFromOtherReducer: someVal });
};
答案 2 :(得分:6)
React Redux适用于单向数据流。
Action ---> Reducer /store ---> Reducer
Reducer适用于小型商店子集,您无法访问不属于Reducer的reducer内的存储区。您可能需要根据reducer状态返回从组件中触发新操作。