More fun with Hooks

James Scalise
Nov 7, 2020

Further my studies in React, I’ve been learning more and more functionality that can be applied to future React projects.

For example, useReducer! With this React function, one can rather simply handle associating state with a reducer function. Like here:

const [stories, dispatchStories] = React.useReducer(

storiesReducer,

{ data: [], isLoading: false, isError: false }

);

We’re seeing stories receiving a default state (the second argument) and being associated with a reducer function (storiesReducer). Instead of having to call upon stories reducer, we now simply have to call dispatchStories and send in the relevant reducer command.

There’s also useCallback. This creates a memoized function every depend its dependency array changes. This allows you to reuse the function in other parts of the application, such as a generalized fetch function.

--

--