9

Is there a way in react router that whenever some components renders the url changes accordingly.

One way which I know is to place window.history.pushState('', '', '/componentUrl'); in the render function of the component

Is there any better way to accomplish it within the route other than path props in Route because path= 'someurl' means whenever someurl is accessed someComponent should render

But in the other way round whenever someComponent renders urls should change to someurl I'm taking this senerio in context of conditional rendering inside a component.

4
  • how are you rendering that component Commented Aug 16, 2017 at 7:57
  • I'm rendering different components in a component on the basis of state like if state == 'a' render Acomponent if state == 'b' render Bcomponent by this way component is rendering but url is not changing Commented Aug 16, 2017 at 8:26
  • The URL should drive the rendering of components. The app state and components shouldn't drive the URL. I'd instead look at changing the URL to then render this component. Commented Aug 16, 2017 at 10:12
  • I wouldn't recommend changing the URL when the component is rendered Consider a case where you are rendering this component within nested child of another component, You would change the URL and now suppose you have the same component reused 3 different points then you would be changing the URL from 3 different components which is incorrect. Instead you should let URL drive your component rendering Commented Aug 23, 2017 at 11:21

1 Answer 1

1

The most important thing to know when using react-router is that:

The path attribute defines the route URL and component attribute defines the component for this route.

I would suggest having a read through this. Best practice suggest, just like Matt Derrick suggested in the comments that the URL should drive the rendering of components.

Since you have Acomponent and Bcomponent that render based on the state I would suggest you then have 2 different URLs to accompany that. So when state changes to be ==='a' then you will jump to /aURL, when the state changes to be ==='b' then you will jump to /bURL.

Hope this answers your question.

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

4 Comments

dats cool jump to /aUrl mean window.history.pushState('', '', '/aUrl) ??
yes or in the appRoutes initialise something like { path: '/aUrl', component: Acomponent }
this is what I have done , looking for a solution like pass a callback function in the router itself which will execute when a component renders , so that this jump url will be handled by the router itself . I don't know weather it is possible with react-router-dom .
In that case you will need something like state in the router. Maybe have a look at this. npmjs.com/package/react-state-router

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.