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

192
Vistas
Can't find the way to useSelector from redux-toolkit within event handler and pass params to it

There is an event handler for click and when it triggered i want to pull specific data from redux using selector where all logic many-to-many is implemented. I need to pass id to it in order to receive its individual data. Based on rules of the react the hooks can be called in function that is neither a React function component nor a custom React Hook function.

So what is the way to solve my problem ?

const handleMediaItemClick = (media: any): void => {
    // For example i check media type and use this selector to pull redux data by id
    const data = useSelector(playlistWithMediaSelector(imedia.id));
};
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

As stated in the error message, you cannot call hooks inside functions. You call a hook inside a functional component and use that value inside the function. The useSelector hook updates the variable each time the state changes and renders that component.

Also, when you get data with useSelector, you should write the reducer name you need from the redux state.

 const CustomComponent = () => {
      // The data will be updated on each state change and the component will be rendered
      const data = useSelector((state) => state.REDUCER_NAME);

      const handleMediaItemClick = () => {
          console.log(data);
      };
 }

You can check this page for more information.https://react-redux.js.org/api/hooks#useselector

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should probably use local state value to track that.

const Component = () => {
  const [imediaId, setImediaId] = useState(null);

  const data = useSelector(playlistWithMediaSelector(imediaId));

  function handleMediaClick(id) {
    setImediaId(id)
  }

  useEffect(() => {
    // do something on data
  }, [imediaId, data])

  return <div>...</div>
}

Does that help?

EDIT: I gather that what you want to do is to be able to call the selector where you need. Something like (considering the code above) data(id) in handleMediaClick. I'd bet you gotta return a curried function from useSelector, rather than value. Then you would call it. Alas, I haven't figured out how to that, if it's at all possible and whether it's an acceptable pattern or not.

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