reactjsreduxreact-router-domhigh-order-component

Parent component re-mounting when react-router switching links


I have main layout component and normally when i change route links {this.props.child} needs to be change only.

enter image description here

This is my layout component. index.js

<React.Fragment>
    {this.props.settings.dark_overlay ? <div className="dark"></div> : ''}
    <div className="site">
        <Header />
        <div className="sitecontent">
            <div className="container flex-or">
                <Content>
                    {this.props.children}
                </Content>
                {this.props.AsideType === "settings" ? null : <React.Fragment><AsideFirst /><AsideSecond /></React.Fragment>}
            </div>
        </div>
        <Footer />
    </div>
</React.Fragment>

When I build my application asidefirst gets data from server, looks like this;

AsideFirst Component

constructor(props) {
    super(props);
    this.state = {
        trends: []
    }

    this.handleButons = this.handleButons.bind(this)
}

handleButons(e){
    let type = e.target.name
    if (type === "refresh") {
        this.getTrend()
    }
}

componentDidMount() {
    this.getTrend()
}

getTrend() {
    axios.post('/api/data/title/trend')
        .then(res => {
            const trends = res.data;
            console.log(trends)
            this.setState({ trends: trends });
        })
}

No problem for here...

But,

When I change routes component re-creating again, Here is my app.js*

<React.Fragment>
<ReactNotification />
<Router history={history}>
    <Switch>
      <Route path="/" exact component={Index} />
      <Route path="/kayit/" exact component={Register} />
      <Route path="/giris/" exact component={Login} />
      <Route path="/aktivasyon" exact component={Activation} />
      <Route path="/hesap/ayarlar" exact component={AccountSettings} />
      <Route path="/cikis" exact component={Logout} />
      <Route path="/:string/" exact component={ComplexPush} />
      <Route component={Page404} />
    </Switch>
</Router>
</React.Fragment>

My layout component (so AsideFirst) is getting data from server every route changes although route components are child components. Example route component;

ComplexPush Component

<Layout>
    <div className="complex-header">
        <Link to="/"><h4>{str}</h4></Link>
        {!id ? <NoEntry /> : "" }
    </div>
    <Entry />
    <Button variant="primary">Gönder</Button>
</Layout>

So, Asidefirst(inside Layout) is mounting all the time when route change although my all parent component Layout. I don't want this. How can i solve this ?


Solution

  • Supposing we have the Layout component.

    this would be u routing with-in the Layout component:

    <React.Fragment>
      <ReactNotification />
      <Router history={history}>
        <Layout>
          <Switch>
            <Route path="/" exact component={Index} />
            <Route path="/kayit/" exact component={Register} />
            <Route path="/giris/" exact component={Login} />
            <Route path="/aktivasyon" exact component={Activation} />
            <Route path="/hesap/ayarlar" exact component={AccountSettings} />
            <Route path="/cikis" exact component={Logout} />
            <Route path="/:string/" exact component={ComplexPush} />
            <Route component={Page404} />
          </Switch>
        </Layout>
      </Router>
    </React.Fragment>
    

    Where in Layout, u maintain the state u wanna keep, u render this routes as in render props and also all the stuff like the Header ...etc.