Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
Dispatch outside component

I want to dispatch outside component. I want to use option 2 from this link [https://daveceddia.com/access-redux-store-outside-react/][1]. My code look like this

const loginUser = async (data) => {
    return axios.get(url + "/sanctum/csrf-cookie").then(() => {
        axios.post(url + '/api/login', data)
            .then(res => {
                return res.data

            })
            .catch((err) => {
                console.log(err);

            })
    })
}
export const handleLogin = (data) => async (dispatch) => {
    console.log('test');
    try {
        const user = await loginUser(data);
        dispatch(actions.setUser(user));
    } catch (err) {
        console.log(err);
    }
}

And into my component

    const test = (e) => {
        e.preventDefault;
        handleLogin({email: 'test@test.pl', password: 'password'})
    }
    return (
        <div className="container">
            <h2>Login</h2>
            <form onSubmit={handleSubmit(test)}>
//...

It doesn't finish code and it may contain mistakes but currently the most important for me is why this code doesn't work and if sometimes is wrong why doesn't show any error. I think that problem is in sync(dispatch). In this example I add console.log for test and it wasn't display. Without that function display console.log. Redux thunk is added to the store too

const store = createStore(allReducers, composeWithDevTools(applyMiddleware(thunk)))
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

import store and use like this :

  store.dispatch(actions.setUser(user));

and you can get state out of component with :

store.getState().items
about 4 years ago · Juan Pablo Isaza Denunciar

0

you did not dispatch your actions in your component and in your action js . you can call an action in component by props and dispatch and I could not see the props so I use useDispatch and call handleLogin action there.

use this in action.js file:

 const user = await loginUser(data)(dispatch);

instead :

   const user = await loginUser(data);

then in component:

   import {useDispatch} from "react-redux";
    

    const dispatch = useDispatch();
     const test = (e) => {
        e.preventDefault;
        dispatch(handleLogin({email: 'test@test.pl', password: 'password'}))
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda