5

I would like to know how to use redux sagas with react hooks, how does redux get implemented, is there any unique use cases to know about?

Should I be using the regular react dispatch?

Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75

1 Answers1

5

I had difficulty finding this information out, so I created a project for it to better understand how to use the hooks with sagas.

https://github.com/KevinDanikowski/react-sagas-with-hooks-pokemon-sample-app

In particular, this code block will be the most useful, everything else is just the same implementation of regular redux.

...
  const dispatch = useDispatch();
  const pokemon = useSelector(state => {
    return state.pokemons.find(pokemon => pokemon.id === pokemonId)
  });
  const callPokemon = useCallback(
      () => 
        dispatch({
          type: `GET_POKEMON${useSaga ? '_SAGA':''}`,
          pokemonId
        }),
    [dispatch, pokemonId, useSaga],
  )

  useEffect(() => {
    const getPokemon = () => callPokemon();

    getPokemon();
  }, [callPokemon]) 
...
Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75
  • Mind providing a sample of what it changed from? So we don't have to go hunting around? – BBaysinger Jun 01 '21 at 21:19
  • @BBaysinger I'm not sure what you mean, if you're looking for the source file for this it's in https://github.com/KevinDanikowski/react-sagas-with-hooks-pokemon-sample-app/blob/master/src/components/PokemonCard.js – Kevin Danikowski Jun 01 '21 at 22:05