1

I want to make to render children in father component with React Router Dom v6.2.2, that's how I tried to make it but browser shows nothing eventually:

const MainLayout = props => {
    return (
        <div className='main-layout'>
            <Header />
            <div className='main'>
                {props.children}
            </div>
        </div>
    )
}

App.js:

function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Routes>
          <Route exact path="/" render={() => (
            <MainLayout>
              <Homepage />
            </MainLayout>
          )} />
        </Routes>
      </BrowserRouter>
1
  • Hi, did the below answer solved your problem? Commented Sep 14, 2022 at 8:04

2 Answers 2

2

You should use element property of Route to render your component since you are using React Router Dom v6, component and render are for anterior versions, or simply wrap it inside Route:

export default function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Routes>
          <Route exact path="/">
            <MainLayout>
              <Homepage />
            </MainLayout>
          </Route>
        </Routes>
      </BrowserRouter>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

1
function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Routes>
          <Route exact path="/" element={<MainLayout />}>
            <Route path="/home" element={<Homepage />} />
          </Route>
        </Routes>
      </BrowserRouter>
    </div>
  )
}

and Add a Outlet component inside MainLayout Component

const MainLayout = props => {
  return (
    <>
      <div className="main-layout">
        <Header />
        <div className="main">{props.children}</div>
      </div>
      <Outlet />
    </>
  )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.