From 'object inside a snap' to 'some type of path' (to 'the same object inside a different snap') #574
Closed
DavidMulder0
started this conversation in
General
Replies: 1 comment 4 replies
-
I would simply use {state.arrayOfObjects.map((obj) => (
<SomeComponent obj={obj}></SomeComponent>
))} and inside |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Whilst exploring the possibility of using valtio inside of a different framework I was thinking about the case where a parent component want to pass a part of the state to a child component. Let's say something like
The problem though is that anytime anything else changes in the state, this will cause
obj
to be different, thusSomeComponent
will rerender. One solution would be to always pass in the index, soAnd then inside
SomeComponent
to gosnap.arrayOfObjects[props.obj]
, which would work, but is messy to do in practise as you now need to duplicate the logic regarding 'where' in the store the data is in two places.I wondered whether it would be possible (and make sense) to define a pair of functions that would work as follows:
And then inside
SomeComponent
do something likefromReference(snap, props.obj)
, whereprops.obj
would be a primitive like'arrayOfObjects.1'
. That waySomeComponent
at least doesn't need to know anything. (Plus, as an added bonus one might in practise abstract thetoReference
andfromReference
away depending on the framework, thus making it 'just work')Looking at the code of
proxy-compare
I was able to hack such a function together fairly quickly, but I really would love some input on whether the approach makes any sense at all and/or whether there are any better already existing approaches? (I tried searching around, but without much luck).Beta Was this translation helpful? Give feedback.
All reactions