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

134
Views
ReactJS: Update parent component className or recreate it in child parent with different className

I'm writing an application with ReactJS (with react-router-dom and react-bootstrat). My switch changes the content of the vDOM while still maintaining a same <Container> ... </Container>, but depending on the page it changes the classes used.
I'm stuck trying to figure out which approach is correct:


This one uses useState to update the "parent" component from the "child" components:

// Pages.js
export default function Pages() {
    const [classes, setClasses] = useState("flex-grow-1")
    const replaceClasses = classes_ => setClasses(`flex-grow-1 ${classes_}`)

    return (
        <Container className={classes}>
            <Suspense fallback={<Loading replaceClasses={replaceClasses} />}>
                <Switch>
                    <Route exact path="/">
                        <Home replaceClasses={replaceClasses} />
                    </Route>
                    <Route path="/404">
                        <NotFound replaceClasses={replaceClasses} />
                    </Route>
                    <Redirect to="/404" />
                </Switch>
            </Suspense>
        </Container>
    )
}

// Home.js
export default Home({ replaceClasses }) {
    // Replace root container classes
    replaceClasses('d-flex align-items-center justify-content-center')
    return <span>Home Page</span>
}

// And so on...

This one, instead, recreates the Container with different classes in each "child" component:

// Pages.js
export default function Pages() {
    return (
        <Suspense fallback={<Loading />}>
            <Switch>
                <Route exact path="/">
                    <Home />
                </Route>
                <Route path="/404">
                    <NotFound />
                </Route>
                <Redirect to="/404" />
            </Switch>
        </Suspense>
    )
}

// Home.js
export default Home() {
    return (
        <Container className="flex-grow-1 align-items-center justify-content-center">
            <span>Home Page</span>
        </Container>
    )
}

// And so on...
about 4 years ago · Juan Pablo Isaza
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!