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

72
Views
Pass prop from intermediate component into props.children

I'd like to make a functional component which gives a prop to its immediate children. I don't want to use a context because I want it to only be available to the immediate children.

So given the following components:

const TopLevel = () => {
  return (
    <FooGiver>
      <LowerLevel />
    </FooGiver>
  )
}

interface LowerLevelProps {
  foo?: string;
}
const LowerLevel = ({foo}: LowerLevelProps) => {
  return foo ? <p>{foo}</p> : <p>No foo provided</p> 
}

const FooGiver = ({ children }: FooGiverProps) => {
  const foo = bar // TODO: This should set its children's `foo` prop to "bar"
 
  return (
    <>
      { children }
    </>
  )
}

Is it possible to define FooGiver such that it will give any children it receives a foo prop of "bar"?

If so, would I need to continue to keep the foo prop optional, or would TS pick up that FooGiver will always set foo, and not complain that I called <LowerLevel /> without providing the required foo prop?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think this would do it to add props to all children

The following example component adds a new props named newProp with value true to all children it takes.

function Parent({children}){
    return <>

        {children.map(child=>{
            const modified = child;
            modified.props["newProp"] = true;//add new value to prop
            return modified
        })}

    </>
}
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!