sidnt / zarn

zio & react native
0 stars 0 forks source link

react context primer #5

Open sidnt opened 4 years ago

sidnt commented 4 years ago

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

pass data? is it actually passing data or is it just passing references to some object in the shared runtime?

props that are required by many components within an application

Context is primarily used when some data needs to be accessible by many components at different nesting levels.

image

so a prop might be required by a random selection of components in this tree. if it is just one component down somewhere, instead of using context to dereference props, an alternative design could have a constructed component, preconstructed with the aforementioned props, passed down as is.

Context provides a way to share

  • values like these between components without having to explicitly pass a prop through every level of the tree.
  • data that can be considered “global” for a tree of React components

an initial context is created and wrapped around components that need to dereference values from it

All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes.

sidnt commented 4 years ago

the access pattern is like zio environment. components inside can do global* dereferencing.