Logo

dev-resources.site

for different kinds of informations.

React State Management: When & Where add your states?

Published at
6/30/2024
Categories
react
state
useref
Author
atenajoon
Categories
3 categories in total
react
open
state
open
useref
open
Author
9 person written this
atenajoon
open
React State Management: When & Where add your states?

When you start learning React, managing state can be challenging at first. It's crucial to understand when you really need a state for a variable and where to place that state to ensure your code is robust and efficient. Proper state management not only optimizes performance by minimizing unnecessary re-renders but also enhances predictability and maintainability, making your code easier to debug. It promotes component reusability, supports application scalability, and maintains a clear separation of concerns. Ultimately, effective state management leads to a smoother user experience and a high-quality, performant application.

There is a series of questions you can ask yourself to determine if your variables need separate states or if they can be simple constants. Additionally, these questions can help you decide where to keep the state if it is needed.

When do I need a state variable?

Do you need to store data?
YES:
Will data change at some point?
NO: Regular "const" variable
YES:
Can it be computed from existing state/props?
YES: Derive state
NO:
Should it re-render the component?
NO: Ref(useRef)
YES:
PLACE A NEW PIECE OF STATE IN COMPONENT!

Where to place your new state?

Always start with a local state in the current component. Then ask yourself if it's:
Only used by this component?
YES: Leave it in the component
NO: Also used by a child component?
YES: pass it to the child via props
NO: Also used by one or a few sibling components?
YES: List state up to first common parent
NO: Used all over the component tree by more than a few sibling components?
YES: Then, you probably need a Global State!

useref Article's
29 articles in total
Favicon
How to create components interfaces using useRef/forwardRef/useImperativeHandle
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
Mastering React's useRef Hook: Working with DOM and Mutable Values
Favicon
Understanding useRef: A Beginners Guide
Favicon
useRef Hook Explained
Favicon
useRef() in React . . .
Favicon
React State Management: When & Where add your states?
Favicon
Unlocking the Power of useRef: Besic to Advanced Examples for React Developers
Favicon
Leveraging useRef in TypeScript for Efficient DOM Manipulation
Favicon
Unlocking the Power of React Hooks
Favicon
React.useRefの理解を含めるエクササイズ
Favicon
Mastering React's useRef Hook: A Deep Dive
Favicon
Unlocking the Power of useRef in React: 12 Essential Use Cases
Favicon
Série React Hooks: useRef
Favicon
Toggle Password Visibility Using React useRef
Favicon
Stop using useState for everything
Favicon
Using React hooks to develop a Video Player
Favicon
React - How to load a form with the latest data
Favicon
React: сфокусировать поле ввода по чекбоксу
Favicon
How do I check/uncheck all checkboxes with a button In React Js using useRef() Hook ?
Favicon
Multiple item using one ref
Favicon
Closures and useEffects
Favicon
I need help. TypeError: Cannot read properties of undefined (reading 'current')
Favicon
Using React useRef Hook to access immediate past props or state.
Favicon
Compare Props in React Functional Components.
Favicon
Creating infinitely scrolling SPA using React
Favicon
useRef()가 순수 자바스크립트 객체라는 의미를 곱씹어보기
Favicon
Complete useRef() hook with live code examples
Favicon
Useful Patterns with React hooks

Featured ones: