When a simple React context gets out of hand.

By DEV Community - 2021-01-28

Description

It seemed like a good idea to start. Separate state into different "contexts". Just use `useState`. Then, `useEffect` and multiple if branches snuck in and it became a nightmare.

Summary

  • The AuthContext contains state associated with authentication.
  • Both the AuthContext and TimelineContext are part of the global state so one solution would be to just combine them, and separate the domains inside the state object.
  • Defining our initial state lets us see all of our global state at a glance.
  • Now, we can set an initialSelectedPost & initialSelectedComment without worrying about the useEffect firing off;

 

Topics

  1. Frontend (0.31)
  2. Mobile (0.17)
  3. Management (0.15)

Similar Articles

React Hooks Cheatsheet: The 7 Hooks You Need To Know

By freeCodeCamp.org - 2021-02-08

This new tutorial will show you everything you need to know about React Hooks from scratch. I've put this cheatsheet together to help you become knowledgeable and effective with React Hooks as quickly ...

Managing state with React Context

By DEV Community - 2020-10-14

In my previous post I was writing about using Redux with React for the state management. This blog po...