2

App.js

function App() {
  <div className="App">
    <Router>
      <Switch>
        <Route exact path="/home" component={Home} />
        <Route exact path="/search" component={Home} />
      </Switch>
    </Router>
  </div>;
}

Home.js

function Home() {
    const location = useLocation();
    return (
        <div className="home">
            <Component1 />
            {location.pathname === "/home" && <Feed />}
            {location.pathname === "/search" && <Search />}
            <Component2 />
        </div>
    );
}

This works perfectly as I want to render the Feed or Search component depending on the URL.

But, I want to know is it okay to use location.pathname or is there any better alternative?

4
  • why aren't you using react routers? Commented Jul 16, 2021 at 16:58
  • 1
    Can you describe what you're trying to do in more detail? How can you be on /search if your are rendering the Home component on /home? Commented Jul 16, 2021 at 17:02
  • @AlexWayne The Home component should probably be named MainLayout or something, and various pages share this layout. Commented Jul 16, 2021 at 17:02
  • @DavidCallanan Ohh yes. Should rename the component. Am terrible at naming components. Hehe! Commented Jul 16, 2021 at 17:07

2 Answers 2

2

You could do something like:

App.js

function App() {
  return <div className="App">
    <Router>
      <Switch>
        <Route exact path="/home" component={() => <Home showFeed/>} />
        <Route exact path="/search" component={() => <Home showSearch/>} />
      </Switch>
    </Router>
  </div>;
}

Home.js

function Home(props) {
    const location = useLocation();
    return (
        <div className="home">
            <Component1 />
            {props.showFeed && <Feed />}
            {props.showSearch && <Search />}
            <Component2 />
        </div>
    );
}

This allows you to abstract away the Home component's dependency on any routing mechanism, and simply allows you to control whether certain elements appear or not from outside this component.

Sign up to request clarification or add additional context in comments.

6 Comments

@zx01 Great. Consider this answer if you'd like more flexibility (if you are always only displaying one of the components at a time).
This will re-render the entire Home component. on url change. Is there any way in which only the Feed and Search components re-render based on url and the other components (Component 1 & 2) don't render and remain as it is?
@zx01 What's wrong with a re-render? I'm pretty sure (correct me if I'm wrong) that the state will remain intact between re-renders, so there should be no issue.
There is nothing wrong. But am just curious whether there is any way to do that?
@zx01 No I don't think so. But even when a re-render occurs on the virtual dom, React will do a diff, so the elements shouldn't actually get re-created in the real dom.
|
2

use home component as layout. This can be highly recommended. You can rename your home component as Layout. This is more flexible way.

function Home() {
    const location = useLocation();
    return (
        <div className="home">
            <Component1 />
             { props.children }
            <Component2 />
        </div>
    );
}

In your app.js modify like bellow

function App() {
  <div className="App">
    <Router>
      <Switch>
        <Route exact path="/home">
          <Home>
           <Feed />
          </Home>
         </Route>
        <Route exact path="/search">
          <Home>
           <Search/>
          </Home>
         </Route>
        </Route>
      </Switch>
    </Router>
  </div>;
}

1 Comment

its glad to help

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.