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

194
Vistas
How to dispatch without connect

I want to dispatch without a component. I made this code:

const handleClient = () => (dispatch) => {
  try {
    dispatch({
      type: "SET_CLIENT",
      user: {
        id: 1,
        name: "client",
      },
    });
  } catch (err) {
    console.log(err);
  }
};
export default handleLogin;

But this code doesn't work. I import this file and use that in onSubmit event. I use Redux-Thunk. @Edit Look at this code from tutorial

export const fetchContacts = () => (dispatch) => {
  fetch("https://myapi.local/contacts)
    .then(res => res.json())
    .then(json => dispatch(contactsFetched(json.results)));
};

I done something similar and it works so I don't understand that in this example it doesn't work. I tryed other method

import actions from "./actions";
import store from "../store";
const handleClient = () =>  {
    try{
        
        store.dispatch(actions.setClient({id: 1, name: 'Hello'}));
    } catch(err){
        console.log(err);
    }
}
export default handleClient;

but it doesn't work too.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

https://redux.js.org/api/store#dispatchaction

You need to have access to your instance of the redux store, which you created somewhere in your codebase via createStore(). Then it's just store.dispatch(handleclient()).

Please keep in mind that this is usually a rare requirement and potentially a code smell. Why is your thunk not triggered from within a React component?

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should never use store.dispatch, so your first example is incorrect. Your second example is better, but the only place where you can use useDispatch is inside a function component or a custom hook, you cannot use it inside a callback like handleLogin. Try to use useDispatch at the beginning of your React component and then pass the dispatch function as an argument to handleLogin.

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