I have a React app with Redux and Redux Toolkit. Until now I only used redux-related code in React components using useDispatch and useSelector.
Now I want to update the Redux store from a pure JavaScript module. The actual module saves user data (notes, rewards for example) into an IndexedDB. I would like to update my redux store as well from this module. However, as it is not a React component I can not use useDispatch.
I have created a slice like this:
const initialState = {
rewards: []
};
export const rewardSlice = createSlice({
name: 'reward',
initialState: initialState,
reducers: {
setRewards: (state, action) => {
state.rewards = action.payload.rewards;
},
addReward: (state, action) => {
state.rewards = [action.payload.reward, ...state.rewards];
},
},
});
export const { setRewards, addReward } = rewardSlice.actions;
export default rewardSlice.reducer;
And what I want is the equivalent of this code without using react hooks:
const dispatch = useDispatch();
dispatch(addReward({ reward: userReward }));