最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nested React Router : hide parent component on showing nested child component - Stack Overflow

programmeradmin7浏览0评论

Being a beginner in reactJS, I want to know how to hide parent ponent when i route to child ponent URL

Assume a scenario:

User is at "/house" as this:

<Route path="/house" ponent={House} />

when user clicks a house grid he navigates to "/house/flat/:flatID". Inside House ponent

<Route
    path={`${this.props.match.url}/flat/:flatId`}
    render={() => <div>Each Flat details</div>}
/>

then both child and parent ponents are visible like this:

So what i want is to show only flat ponent when user navigates to "/house/flat:FlatId". Please suggest me anything that helps ! Any links to article so that i can learn and achieve such functionality.

Code:

App.js

<Switch>
    <Route exact path="/" ponent={Home} />
    <Route exact path="/account" ponent={Account} />
    <Route path="/gharjagga" ponent={GharJagga} />
</Switch>

House.js

onGharGridClick= id => {
    <Redirect to={`${this.props.match.url}/flat/${id}`} />;
};

return (
    <Route
        path={`${this.props.match.url}/flat/:fkatId`}
        render={() => <div>Ghar Each</div>}
    />
);

Being a beginner in reactJS, I want to know how to hide parent ponent when i route to child ponent URL

Assume a scenario:

User is at "/house" as this:

<Route path="/house" ponent={House} />

when user clicks a house grid he navigates to "/house/flat/:flatID". Inside House ponent

<Route
    path={`${this.props.match.url}/flat/:flatId`}
    render={() => <div>Each Flat details</div>}
/>

then both child and parent ponents are visible like this:

So what i want is to show only flat ponent when user navigates to "/house/flat:FlatId". Please suggest me anything that helps ! Any links to article so that i can learn and achieve such functionality.

Code:

App.js

<Switch>
    <Route exact path="/" ponent={Home} />
    <Route exact path="/account" ponent={Account} />
    <Route path="/gharjagga" ponent={GharJagga} />
</Switch>

House.js

onGharGridClick= id => {
    <Redirect to={`${this.props.match.url}/flat/${id}`} />;
};

return (
    <Route
        path={`${this.props.match.url}/flat/:fkatId`}
        render={() => <div>Ghar Each</div>}
    />
);
Share Improve this question edited Nov 10, 2018 at 6:03 Efrain Sanjay Adhikary asked Nov 10, 2018 at 5:34 Efrain Sanjay AdhikaryEfrain Sanjay Adhikary 3255 silver badges24 bronze badges 2
  • You want switch route right? Or Please share you route s code – Hafeez Hamza Commented Nov 10, 2018 at 5:39
  • when user clicks any grid he navigates to /house/flat/flatID then what i want to show in this url is only flat details, instead both House and flat Details are showing. – Efrain Sanjay Adhikary Commented Nov 10, 2018 at 5:46
Add a ment  | 

2 Answers 2

Reset to default 18

You can achieve it different ways

  1. define routes in the parent ponent, I think this is the best option.
<Switch>
  <Route path="/account" ponent={Account} />
  <Route path="/house/flat/:flatId" ponent={FlatComponent}/>
  <Route path="/house" ponent={House} />
  <Route path="/" ponent={Home} />
</Switch>

Note: instead of using exact, order your routes based on priority, that will make the route to redirect to next matching route if any typo in the route entered

  1. You can make House as separate route ponent, and nest the routes inside that ponent
// Routes
<Switch>
  <Route path="/account" ponent={Account} />
  <Route path="/house" ponent={House} />
  <Route path="/" ponent={Home} />
</Switch>

// House ponent
class House extends React. Components {
  render() {
    return (
      <Switch>
        <Route path="/house/flat/:flatId" render={() => <div>Each Flat details</div>} />}/>
        <Route path="/house" ponent={HouseGridComponent} />
      </Switch>
    )
  }
}
  1. you can check whether the route has flatId and hide the elements, in your House ponent you can check this.props.match.params.flatId, if the flatId is set you can hide that House Component.
// House Component

class House extends React. Components {
  render() {
    return (
      <div>
        {
          !this.props.match.params.flatId?
            <h1>House Component</h1>:
            null
        } 

        <Route path={`${this.props.match.url}/flat/:flatId`} render={() => <div>Each Flat details</div>} />
      </div>
    )
  }
}

The solution is to raise "/house/flat/:flatId" to the same level as "/house".

<Switch>
  <Route exact path="/" ponent={Home} />
  <Route path="/account" ponent={Account} />
  <Route path="/house/flat/:flatId" render={() => <div>Each Flat details</div>}/>
  <Route path="/house" ponent={House} />
</Switch>
发布评论

评论列表(0)

  1. 暂无评论