Replies: 15 comments
-
Good question.
This is actually fine in terms of fine-grained re-renders.
I would do that. I don't have other patterns. Wonder if anyone has other ideas.
That's a great idea. Let's create a wiki page. Would you like to work on it?
You are welcome! |
Beta Was this translation helpful? Give feedback.
-
Thank you for the answer! I ended up making a CodeSandBox in order to test the different possibilities I had. My conclusion is that even though the My initial hope was to only re-render the child that was updated but it's not possible. Not because of Valtio but because of the way React itself behaves. Since Valtio will bubble-up anyway, my conclusion is to use the Regarding the wiki page, it's a good idea but I don't really feel like redacting a structured page. Maybe this issue + my sandbox can be a good placeholder until someone feels like making the page? |
Beta Was this translation helpful? Give feedback.
-
Sounds good. Just a little correction: valtio not only bubbles up the event, but also creates a new array on snapshot. It's safe to mutate array items, from the React perspective too. const state = proxy({ arr: [{ id: 1, text: 'hello' }, { id: 2, text: 'world' }] })
const snapArr1 = snapshot(state.arr)
state.arr[1].text = 'react'
const snapArr2 = snapshot(state.arr)
snapArr1 !== snapArr2 // is true |
Beta Was this translation helpful? Give feedback.
-
Thanks but what about the following, though? const state = proxy({ arr: [{ id: 1, text: 'hello' }, { id: 2, text: 'world' }] })
const snap1 = snapshot(state)
state.arr[1].text = 'react'
const snap2 = snapshot(state)
snap1 !== snap2 // is it true? |
Beta Was this translation helpful? Give feedback.
-
Yes, it's still |
Beta Was this translation helpful? Give feedback.
-
This is a good discussion for docs, I've saved it, so I think it's safe to close this.. (now or when the docs go up is fine too) |
Beta Was this translation helpful? Give feedback.
-
First of, I want to say this is awesome tech and we are migrating to it from jotai and very excited about it. On this discussion topic though, I disagree with "My initial hope was to only re-render the child that was updated but it's not possible. Not because of Valtio but because of the way React itself behaves" React renders down, not up and not sideways so if I have a very big nested state with many nested components accordingly, this means every time each element in the bottom changes it will make my entire page rerender from the top level array. This would be a disaster for me, even more so when considering the whole point was to avoid uneeded renders. However, valtio awesomeness can make it work exactly like I want it, it just took me some time to understand how. There's a related topic that's lacking in the docs for me which is how to work with nested components and valtio. If that is welcome, I'll be happy to give a go at writing that wiki page, I can also show the "right" and "wrong" using codesandbox and show how the render count is different for different implementations |
Beta Was this translation helpful? Give feedback.
-
@idanz Hi and thanks for your message! I think it would be nice if you could elaborate. Maybe write a big comment detailing what you are thinking about and this comment may be upgraded to a wiki page one day! |
Beta Was this translation helpful? Give feedback.
-
Sure thing, I'll write a draft here in a couple of days. The first item is how to work with nested components and then to show that arrays are just a special case of nesting and work just as well. |
Beta Was this translation helpful? Give feedback.
-
Hey so I came up with a draft that is all inside this csb: https://codesandbox.io/s/handling-nesting-and-arrays-in-valtio-hoe3j4?file=/src/App.js |
Beta Was this translation helpful? Give feedback.
-
@idanz It looks good (disclosure: I just glimpsed it). Nice work! ping @RikuVan for another review. |
Beta Was this translation helpful? Give feedback.
-
Yes there's a few points there, specifically for the map array of objects to child components, how to avoid many renders in complex objects etc. |
Beta Was this translation helpful? Give feedback.
-
@dai-shi it's been some time, anything I can do to help push this forward? really just looking to support to project |
Beta Was this translation helpful? Give feedback.
-
You can summarize you idea here. Or, directly create a new doc and open a PR. I think the example should be more simplified. |
Beta Was this translation helpful? Give feedback.
-
What is the proper way to update a specific value inside an array without knowing its index?
I'm migrating to Valtio and I was doing it like so:
As you can see, I create a whole new array. I'm guessing this will be considered as an update of the whole thing.
Is it possible to make it trigger an update only for a single element?
For example, if I were to perform a
findIndex
and thenspells[foundIndex] = newSpell
?Trying to figure out the best practice for this case.
(More generally, I think arrays are specific enough that a dedicated section in the docs would be quite welcome and useful.)
Thanks in advance for answering me!
Beta Was this translation helpful? Give feedback.
All reactions