DEV Community

Discussion on: ⚛️ 🚀 React Component Patterns

Collapse
 
velenir profile image
Dmitry

useDogProviderState will never throw because even if there is no DogDataProvider above the component, React.useContext(DogDataProviderContext) will return the default value initState

If anyone is interested, I rewrote the Flexible Compound Components example with functional components instead of classes: sandbox

Collapse
 
alexi_be3 profile image
Alexi Taylor 🐶

Thank you for pointing this out. I have updated the article with changes to Provider Pattern and added examples for Compound Components and Flexible Compound Components with functional components.

Collapse
 
hatrunghung profile image
Ha Trung Hung

Actually the purpose of useDogProviderState is to make sure that the consumer of the DogDataProviderContext will be under the DogDataProvider in the React tree in order to consume the value provided from the provider. They would not want something like: const App = () => <Profile /> :D
Actually there's a discussion about this on Twitter: twitter.com/kentcdodds/status/1125...