using same component for different route path in react-router v4

后端 未结 4 1847
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 19:19

I am trying to have separate routes but same component for add/edit forms in my react app like the below:


        

        
4条回答
  •  无人及你
    2020-12-29 19:51

    One solution is use inline function with component, that will render a new component each time, But this is not a good idea.

    Like this:

     }>
     }> 
    

    Better solution would be, use componentWillReceiveProps lifecycle method in ManageClient component. Idea is whenever we render same component for two routes and switching between them, then react will not unmount-mount component, it will basically update the component only. So if you are making any api call or require some data do all in this method on route change.

    To check, use this code and see it will get called on route change.

    componentWillReceiveProps(nextProps){
       console.log('route chnaged')
    }
    

    Note: Put the condition and make the api call only when route changes.

提交回复
热议问题