I need to make redux action and then use state to update my url. I have tried to make a promise:
const opa = (type: string, checked: boolean) => (
dispatch: any,
getState: any ) =>
Promise.resolve().then(() => {
return dispatch(sidebarEmploymentTypeActions.setType(type, checked))
})
And then use it:
const handleEmploymentTypeChange = (type: string, checked: boolean) => {
//@ts-ignore
dispatch(opa(type, checked)).then(console.log(employmentType))
}
But console.log returns previous state. My action:
export const setType = (type: string, checked: boolean) => {
return {
type: sidebarEmploymentType.SET_TYPE,
employmentType: type,
checked
}
}
And reducer:
const sidebarEmploymentType: Reducer = (
state = defaultState,
{ type, employmentType, checked }
) => {
switch (type) {
case sidebarEmploymentTypeType.SET_TYPE:
return {
...state,
[employmentType]: {
checked: checked,
label: state[employmentType].label,
},
}
default:
return state
}
}
export { sidebarEmploymentType }
Root reducer:
export const rootReducer = combineReducers({
sidebarEmploymentType
})
And store creation:
import thunk from 'redux-thunk'
const store = createStore(
persistReducer(persistConfig, rootReducer),
composeEnhancers(applyMiddleware(thunk))
)