-
Could you provide an example of how to use this library with react while initializing state with props? Would it require the use of react context or is it possible without it? Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Jan 21, 2022
Replies: 1 comment
-
If a proxy state is defined at module level, it's already initialized, so you should basically change it with const state = proxy({ text: 'hello' })
const Component = ({ initialText }) => {
useEffect(() => {
state.text = initialText
}, [])
// ...
} If this is not something you are looking for, a possible pattern is lazy initialization in render (which requires deeper understanding), or use context: https://github.com/pmndrs/valtio/wiki/How-to-use-with-context |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
nratter
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If a proxy state is defined at module level, it's already initialized, so you should basically change it with
useEffect
.If this is not something you are looking for, a possible pattern is lazy initialization in render (which requires deeper understanding), or use context: https://github.com/pmndrs/valtio/wiki/How-to-use-with-context