Georg Unterholzner @georg.dev · Mar 5

React: passing state from a wrapper component to a child. How? I'm building a library that exposes a single wrapper component. Users of this library add the wrapper to their TSX and nest their child component inside it. The wrapper fetches some data that's relevant to the child component.

1 likes 3 replies

?

Replies

Georg Unterholzner · Mar 5

The first thing that comes to mind is adding a callback to the wrapper, allowing the user to retrieve the state. However, this feels a bit boilerplate-y. Every user would now have to manually wire the two components together. I’m not a fan of that.

Bao Huynh · Mar 5

Hmm I think this's a problem with no truly elegant solution because data can't just appear in the child component without explicit data flow. One way could be have a hook "useWrapper" that returns the "data" and then child component explicitly uses this "data" as props (like react-hook-form)

Bao Huynh · Mar 5

Maybe render props pattern ? I think that's what Formik uses