Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
How to transfer props to children's children in React?

I'm transferring some props from a layout to every children. This works when the child is a direct layout's children, but it doesn't work when I wrap a layout's child in a, for example, div.

Working case:

<Layout >
  <SectionA  language={props.language}  />
</Layout>

But it doesn't in this case:

<Layout >
   <div>
     <SectionA  language={props.language}  />
   </div>
</Layout>

*When I access to props.language in <SectionA /> component, it's undefined

Here's how I'm transferring the props from the layout to its children

render(){

   const children = React.Children.map(this.props.children, (child, index) => {
        return React.cloneElement(child, {
            language: this.state.language,
        });
    });


 return (
        {children}
 )
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This seems like a pretty good case for the useContext hook. This would allow you to pass language down as context to all children of the context provider. For example:

import React, { createContext, useContext } from 'react';

const LanguageContext = createContext();

const LanguageProvider = (props) => {
    // Can set up state here if needed
    return (
        <LanguageContext.Provider value={'defaultLanguage'}>
            {props.children}
        </LanguageContext.Provider>
    );

// Wrap the components that need access to the context in the provider
const App = () => {
    return (
        <LanguageProvider>
            <Layout >
                <div>
                    <SectionA />
                </div>
             </Layout>
        </LanguageProvider>
    );

}


// And to access that context in SectionA (or any other child)
const SectionA = () => {
    const language = useContext(LanguageContext);

    return (<h1>Language is {language}</h1>);
}
about 4 years ago · Juan Pablo Isaza Report

0

I'm pretty sure there is a better way to do this (probably in the layout file), but here is how I solved this problem:

First I used a second hoc to wrap and style the sectionA component:

function simpleWrapper(props) {


return(
    <React.Fragment>
    <div className='container'>
      <SectionA language={props.language}/>  
    </div>
    <div>
      <SectionB language={props.language}/>  
    </div>
     ...
    <style jsx>{`
      .container {
        Stuff
    `}</style>
    </React.Fragment>
)
}

export default simpleWrapper;

And then I transferred all props to this wrapper:

<Layout >
  <SimpleWrapper language={props.language} />

</Layout>

)

Note that I'm using this approach because I have more than only 1 component inside of

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!