Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
Dispatch in pure JS code using React with Redux Toolkit

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 }));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you have a configStore.js file or the likes where you have used configureStore(), you can use the assigned constant using this method and import in the file where you need to call the dispatch() function

for example, supposing your reducers are configured in an exported constant called store:

//Assuming the file name is configStore.js and 
//configureStore is assigned to store
import {configureStore} from '@reduxjs/toolkit';

export default store = configureStore({
      reducer: persistedReducer,
      middleware: getDefaultMiddleware =>
        getDefaultMiddleware({
          serializableCheck: {
            ignoredActions: [
              FLUSH,
              REHYDRATE,
              PAUSE,
              PERSIST,
              PURGE,
              REGISTER,
            ],
          },
        }),
    });

now in the file where you need to dispatch() to store, simply import store. And then destructure from it the dispatch function

import store from './path_to/configStore';
.
.
.
const {dispatch} = store;
dispatch(addReward({ reward: userReward }));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda