larrymyers / react-mini-router

A minimal URL router for React.js
MIT License
282 stars 38 forks source link

Navigate does not cause render if route stays the same and only params change #39

Closed molson504x closed 9 years ago

molson504x commented 9 years ago

I've got a situation where I have to use navigate to the same route but with different parameters. My route is set up as:

var App = React.createClass({
    mixins: [RouterMixin],

    routes: {
        '/preview/:id': 'previewPage',
        ...
    }
...
});

I'm starting at the route /preview/123456 and then using the navigate command to navigate to /preview/789012:

var React = require('react'),
    Navigate = require('react-mini-router').navigate;

var PreviewPage = React.createClass({
    ...
    changeProduct: function(productId) {
        Navigate('/preview/' + productId);
    },
    ....
});

I'm able to see the URL change, however it seems like since I never changed the route and only changed the route parameters, the view never refreshes. I found a work-around where I update the state variable that holds the product information as part of the changeProduct function, but shouldn't Navigate() update the page regardless of the route changing or not?

Thanks!

larrymyers commented 9 years ago

Can you verify that you are hitting the route handler method that creates the PreviewPage component after calling navigate? Navigate should just trigger a render() call on the component that has the RouteMixin applied to it.

Navigate doesn't do any magic, it's still up to the root component of your app (or Store if using a flux implementation) to make sure the current app state matches the current url.

molson504x commented 9 years ago

I can confirm that both of those methods are getting hit... I think I see where I'm messing up. In my PreviewPage component, I'm doing my initial ajax call in my componentWillMount() handler and never updating it in my componentWillUpdate().

Solution was to write a componentWillUpdate() that makes the same ajax call as my componentWillMount() and write a shouldComponentUpdate() method to handle setting state on the callback from my ajax call. Without the shouldComponentUpdate() you essentially DDoS yourself because it'll keep repeating the call.

Thanks for making a good component! I'm marking this as closed.

larrymyers commented 9 years ago

Great. Glad to hear you were able to solve your issue.