Mads Brodt’s Post

View profile for Mads Brodt, graphic

Front-End Lead | Helping front-end engineers level up their skills and land jobs | JavaScript, React, Vue, TailwindCSS

There's many different ways to manage state in React, so pick the one that makes sense for your usecase: 1. Have a piece of local state that's only relevant in one component? A simple useState will do just fine. 2. Need to persist some state across refreshes or be able to link to it (like a filtered view or search query)? Keep it in the URL as query parameters. 3. Have a simple global object you just want to reference from anywhere in the component tree? React Context is probably suitable. 4. Need more control or simply have a lot of global state to manage? Consider reaching for a library like Zustand, Redux or React Query. That's also one of the coolest things about React: Because you can do stuff in so many different ways, there's likely one solution that's optimal for your given problem. The important part is to identify your needs and picking that solution, rather than overcomplicating things right away. 🔥

Muhammad Amjad

18, Full Stack Engineer @ArtiFusion | React | Next.js | TypeScript | Node | GraphQL | Postgres | I build things with the latest technologies

5d

This is so helpful! I personally think redux is overkill for most projects unless you have some really complex state going on. For a really complex state, I've managed to handle the state well using useReducer + context. What are your thoughts? Mads Brodt

Damir K.

Battle-Tested Senior Frontend Web Developer & Team Lead | Expert in SPA & PWA | User-Centric | Team-Oriented | People-First | Solution-Driven

3d

I would like to add: Reducers can be written in plain JS / TS and React and are a really powerful concept on its own to code split and improve predictability testibility and maintainability on its own. Redux or RTK should be "Redux with RTK" since I see 0 sense of not hawing the awesomeness of RTK to remove all the boiler and possibly prevent bad decisions (you can still not use RTK functionality it in a specific use case) and can not imagine using RTK without Redux. React query is now named TanStack query and is currently the only option I know of (this might be changing soon) that works out of the box with Suspense. Someone please feel free to change the last statement for me, I am eagerly awaiting more options.

Like
Reply
Raja Jahanzaib Ashfaq

Software Engineer | React Js | Next Js | React Native | Chakra UI | Typescript | Machine Learning | python | Mongo DB | Node Js

4d

Can also utilize local/session storage or cookies to store data needed after refresh or on other pages/components of the app.

Michael G.

💻 Sr. Software Engineer | React Apps 🚀 | 🌎 Global Fully Remote | Consultant 🤝 |

5d

All good, but React Query is not an state manager but a data fetching manager, that’s a different thing. 🤓

Like
Reply
Piyush Jha

Backend Developer @Ujucode || Learning Full Stack Development 🚀

5d

I learned recoil for state management It's syntax is so easy to understand Should I continue to build apps using recoil or should I learn redux or zustand?

Like
Reply
Cristian Dragusin

SWE | Accelerating Your Dream Projects with MERN Stack and T3 Framework Expertise by Sharing Proven Tips and Insights from My Process

5d

When managing state, try using custom hooks to encapsulate logic and reuse it across components. This will help you keep your codebase clean and maintainable.

Marium Noor

🧑💻| Front-end developer | React | Redux | Tailwind | SASS | MUI | JavaScript | HTML | CSS | GSAP | Git | Firebase | Appwrite | Postman | Insomnia | Figma | Web design

5d

Depends on the scalability of application. I don’t understand why some developers always drag redux and over complicate every project. If your application is small-medium sized and not big enough to manage multiple nested states, context api works great!

Like
Reply
Lejdi Prifti

Senior Software Engineer @ Linfa | ML Practitioner | Web3 Developer

5d

Mads I would also add cookies and local storage for state shared across many sessions. Do you agree?

Alex Belov

Top ☁️ Web Service | SaaS development team: 3k Successes in a decade. Your Success is the next one | Podcast Host | Creator of Fitness & Health marketing tool “Calorie Calculator Cloud”

5d

Hey Mads, have you considered the possible delay in load time while managing large global states with React Context? Makes a difference when site speed is a priority.

Like
Reply
Rohit Kumar

React | JavaScript | Node

5d

The 2nd point is good in terms of user experience but it comes with a pitfall and that too depends on browser to browser. The max safe length for characters in URL is around 2k - 2.1k . And if we stick to chrome then it goes to 32k. Then one need to think very wisely what to store and what not.

See more comments

To view or add a comment, sign in

Explore topics