Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cant access children in component used as element in Route (react-router-dom 6.3.0)

Im using react: 17.0.2 and react-router-dom: 6.3.0.

My current App.js snippet looks like this:

class App extends React.Component {

    render() {
        return (
            <div className="App">
                <Routes>
                    <Route element={<Layout/>}>
                        <Route path="/home" element={<Home/>}  />
                        <Route path="/login" element={<Login/>}  />
                        <Route path="/signup" element={<Signup/>}  />
                    </Route>
                    <Route exact path='/' element={<Intro/>}/> 
                </Routes>
            </div>
        );
    }
}

The route to '/' should not use the Layout component, so I need to exclude it from the context.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Within Layout.js I need to access the children like so:

const Layout = ({ children }) => {
  console.log(children)
  return (
        <div>
            <main>{children}</main>
        </div>
  );
};

But children are undefined in the above example. I can access children in Layout.js when I rewrite App.js to the below snippet, but then '/' is also rendered with the Layout.

class App extends React.Component {
    render() {
        return (
            <div className="App">
                <Layout>
                    <Routes>
                        <Route path="/home" element={<Home/>}  />
                        <Route path="/login" element={<Login/>}  />
                        <Route path="/signup" element={<Signup/>}  />
                        <Route exact path='/' element={<Intro/>}/> 
                    </Routes>
                </Layout>
            </div>
        );
    }
}

export default App;

My question is: How can I access children in Layout.js and render path '/' without the layout.

Thanks!

>Solution :

Route and Routes work a little differently in v6. The Route and React.Fragment components are the only valid children of the Routes component, and other Route components the only valid children of the Route. Layout routes must render an Outlet component for the nested routes to render their contents into.

import { Outlet } from 'react-router-dom';

const Layout = () => {
  return (
    <div>
      <main>
        <Outlet />
      </main>
    </div>
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading