Ili*_*daš 16 javascript reducers reactjs redux react-redux
我不知道如何isLoading从reducerForm.jsreducer中访问一个布尔标志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);
});
};
}
Run Code Online (Sandbox Code Playgroud)
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
};
}
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
这是我使用的地方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>
Run Code Online (Sandbox Code Playgroud)
将状态映射到同一组件中的props
function mapStateToProps(state) {
return {
errorMessage: state.signup.error,
isLoading: state.signup.isLoading,
values: state.form.values
};
}
Run Code Online (Sandbox Code Playgroud)
mar*_*son 23
这在http://redux.js.org/docs/faq/Reducers.html#reducers-share-state上的Redux FAQ中有所介绍:
许多用户后来想尝试在两个reducers之间共享数据,但发现combineReducers不允许他们这样做.有几种方法可以使用:
- 如果reducer需要知道来自另一个状态切片的数据,则可能需要重新组织状态树形状,以便单个reducer处理更多数据.
- 您可能需要编写一些自定义函数来处理其中一些操作.这可能需要将combineReducers替换为您自己的顶级reducer功能.您还可以使用reduce-reducers等实用程序来运行combineReducers来处理大多数操作,还可以为跨越状态切片的特定操作运行更专业的reducer.
- 异步操作创建程序(如redux-thunk)可以通过getState()访问整个状态.操作创建者可以从状态检索其他数据并将其放入操作中,以便每个reducer都有足够的信息来更新其自己的状态切片.
Sam*_*amo 13
reducer无法访问另一个reducer的状态,但是如果你正在使用redux-thunk,你可以在一个动作创建者中这样做.例如,您可以定义这样的动作创建者:
export const someAction = () =>
(dispatch, getState) => {
const someVal = getState().someReducer.someVal;
dispatch({ type: types.SOME_ACTION, valFromOtherReducer: someVal });
};
Run Code Online (Sandbox Code Playgroud)
React Redux适用于单向数据流.
Action ---> Reducer /store ---> Reducer
Run Code Online (Sandbox Code Playgroud)
Reducer适用于小商店的子集,你不能访问不属于Reducer的reducer里面的商店.您可能需要根据reducer状态返回从组件中触发新操作.
| 归档时间: |
|
| 查看次数: |
13804 次 |
| 最近记录: |