Lou
Lou

Reputation: 166

Pass a prop to every child component

Is it possible to pass a prop containing a function to any child ? More precisely I would like to pass a function from a component to every {children} component :

<>
    <Navbar />
    <main>{children}</main>
    <Footer />
</>

Upvotes: 0

Views: 113

Answers (1)

Mohaimin
Mohaimin

Reputation: 702

You should then use something like Context for that.

The props you pass to your Context Provider will be available to all it's children. But you won't get them via props, you need to get them via the Context Consumer.

But the good thing is Context Consuming hooks are available to make it easy to do so.

Upvotes: 3

Related Questions