this post was submitted on 28 Oct 2025
37 points (100.0% liked)

Web Development

4719 readers
83 users here now

Welcome to the web development community! This is a place to post, discuss, get help about, etc. anything related to web development

What is web development?

Web development is the process of creating websites or web applications

Rules/Guidelines

Related Communities

Wormhole

Some webdev blogsNot sure what to post in here? Want some web development related things to read?

Heres a couple blogs that have web development related content

CreditsIcon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 2 years ago
MODERATORS
 

Okay so, context, I've come across this video last night. It's a short comparison between React and Svelte. Point 9 - Shared state (6:20) mentions that React doesn't really have a primitive way to share state between nested components and that you basically need to use something like Redux to get that working.

But... I've been sharing state between nested Components in just React for a while now and didn't know that I can't?? But I also don't remember where I learned to do it, so the chances are high that I just hallucinated up this method as a Junior.

Basically, when I want to share state I just make a new Context and ContextProvider, wrap it around the highest level Component I need it in, and use it lower down in the component tree.
If I need a state, I put the two outputs of the useState hook into the context (which feels nice because when I look through the code, I can see right away which children only read the value in the state and which children actually take the setter and have the capacity to change that state). Sometimes I don't even hand out the actual setter from the state, but a new function that also does some input validation before calling the setState itself. Doing it this way has always felt pretty clean to me.

From the React documentation, it seems to me like that's exactly how you're supposed to use Context. But I've also never seen anyone else do it like that. So is it incredibly ill advised and I've been shooting myself in the foot this whole time?

As a more specific example, my most common use case is that I need to render fetched data in a grid. This data can be filtered, but the component that sets the filter state is either on the same level as the grid (the grid's built in filter menu) or above it (a button that sets a predefined quick filter) or even further above that (a useEffect that looks for query parameters in the URL and sets those before the data is fetched for the first time).
So what I'd do is const [filterModel, setFilterModel] =useState() at the highest level and pass it to <FilterContext value={{filterModel, setFilterModel}}>. Then, I'd just use const {setFilterModel} = useContext(FilterContext) within all the components that write the filter and const {filterModel} = useContext(FilterContext) everywhere where I just need to read the filter, like in the hook that actually fetches my data. Does that make sense? Is there an easier/safer way to do it that doesn't involve adopting yet another external library?

you are viewing a single comment's thread
view the rest of the comments
[–] BlackEco@lemmy.blackeco.com 10 points 2 days ago* (last edited 2 days ago) (2 children)

Using Context is fine, but you have to keep in mind that if any state in a Context is updated, all components that uses that Context will re-render, even if they don't use the state that has been updated.

Redux and Zustand allow to subscribe only to the values you need, reducing the amount of re-renders. There are also library-free alternatives like this one from romgrk.

[–] python@lemmy.world 9 points 2 days ago (1 children)

Oh shoot, I did not know that! :0
That means I should keep Contexts small and all values related to another, instead of having "Page Contexts" that just store all things I need in any child. Good to know!

[–] gwl@lemmy.blahaj.zone 2 points 1 day ago

Exactly!

The one I tend towards is feature contexts