Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 19, 2021 12:03 am GMT

Using the useReducer Hook in React with TypeScript

The useReducer hook is an alternative to the useState hook and is preferable when you have complex state logic or when your next state depends on your previous state.

The useReducer hook accepts a reducer type (state, action) => newState and returns a state object paired with a dispatch method much like Redux.

Now the official useReducer documentation will show you how to define a reducer that accepts actions you will call with a dispatch method.

That is a good use case but I have another example of using useReducer to provide partial updates to state which I find covers the rest of my use cases.

useReducer with actions and a dispatch method

The default example for useReducer is set up for you to create a reducer function and provide it with an action. The action provided should have a type and some value to update the state.

To strongly type this feature with TypeScript we can create an enum with all of our possible action types as well as create an interface for the action.

Below is a fairly contrived example but shows the most basic example possible.

// An enum with all the types of actions to use in our reducerenum CountActionKind {  INCREASE = 'INCREASE',  DECREASE = 'DECREASE',}// An interface for our actionsinterface CountAction {  type: CountActionKind;  payload: number;}// An interface for our stateinterface CountState {  count: number;}// Our reducer function that uses a switch statement to handle our actionsfunction counterReducer(state: CountState, action: CountAction) {  const { type, payload } = action;  switch (type) {    case CountActionKind.INCREASE:      return {        ...state,        value: state.count + payload,      };    case CountActionKind.DECREASE:      return {        ...state,        value: state.count - payload,      };    default:      return state;  }}// An example of using the `useReducer` hooks with our reducer function and an initial stateconst Counter: FunctionComponent = () => {  const [state, dispatch] = useReducer(counterReducer, { count: 0 });  return (    <div>      Count: {state.count}      {/* Calling our actions on button click */}      <button        onClick={() => dispatch({ type: CountActionKind.INCREASE, payload: 5 })}      >        -      </button>      <button onClick={() => dispatch({ type: CountActionKind.DECREASE, payload: 5 })}>+</button>    </div>  );};

useReducer using the Partial type for state updates

The next way of using useReducer is best used when you have a state object where you need to change some values inside of it directly rather than dispatching an action to handle state changes.

This way also has fewer types but does expose the state to be changed directly so take that into account when using this method.

// Interface for our stateinterface LoadingState {  loaded: boolean;  loading: boolean;  error: Error | null;}// The example component that use the `useReducer` hook with our stateconst LoadingComponent: FunctionComponent = () => {  /**    See here that we are using `newState: Partial<LoadingState>` in our reducer   so we can provide only the properties that are updated on our state   */  const [state, setState] = useReducer(    (state: LoadingState, newState: Partial<LoadingState>) => ({      ...state,      ...newState,    }),    {      loaded: false,      loading: false,      error: null,    }  );  useEffect(() => {    // And here we provide only properties that are updating on the state    setState({ loading: true });    setState({ loading: false, loaded: true });    setState({ loading: false, loaded: true, error: new Error() });  }, []);  if (state.loading) {    return <p>Loading</p>;  }  return <p>{state}</p>;};

Conclusion

These are my two methods so check out the other posts below for other methods. It's beneficial to read the wide range of ways to accomplish the same thing.

https://www.newline.co/@bespoyasov/how-to-use-usereducer-with-typescript--3918a332

https://www.sumologic.com/blog/react-hook-typescript/

https://www.benmvp.com/blog/type-checking-react-usereducer-typescript/

My brother and I write all the time about React, TypeScript, R, Data Science, you name it on our blog so check it out:

KoalaTea.io


Original Link: https://dev.to/craigaholliday/using-the-usereducer-hook-in-react-with-typescript-27m1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To