reactjs / react-router-redux

Ruthlessly simple bindings to keep react-router and redux in sync
https://www.npmjs.com/package/react-router-redux
MIT License
7.81k stars 644 forks source link

[Feature Request] Support react-router async features #371

Closed avocadowastaken closed 8 years ago

avocadowastaken commented 8 years ago

Greeting, thanks for great lib that helps me a lot to integrate exist project with redux. But as project grows - new challenges comes.

So, react-router has API to lazy-load routes, which is great and react-router-redux has no problem with it. Implementation was easy, but as I wanted to track loading process of Lazy Components, I faced the problem.

After few minutes I found solution:


// First - Create class decorator

function RouterRoot() {
    return Child => {
        return class RouterChangeHandler extends Component { // TODO: (umidbekkarimov) 4/21/16 - wrap with hoist-non-react-statics
            static displayName = `${Child.displayName || Child.name}RouterChangeHandler`

            static contextTypes = {
                store: PropTypes.any
            };

            componentWillMount() {
                this.context.store.dispatch({
                    type: '@@router/LOCATION_CHANGE_SUCCESS', props: this.props
                });
            }

            componentWillReceiveProps(nextProps) {
                this.context.store.dispatch({
                    type: '@@router/LOCATION_CHANGE_SUCCESS', props: nextProps
                });
            }

            render() {
                return (
                    <Child {...this.props}/>
                );
            }
        };
    };
}

// Then use it on Root Component

@RouterRoot()
export class AppRoot extends Component {
    render() {
        return (
            <div className="root">{this.props.children}</div>
        );
    }
}

// Assure that AppRoot - actually at root

export default {
    path: '/',

    component: AppRoot,

    indexRoute: IndexPageContainer,

    childRoutes: [
        // async routes
    ]
};

As you see implementation is pretty easy and react-router will take care of all asynchronous problems.

So what features it can give:

What problems it can create:

EDIT:

Here logs screenshot of initial loading

screen shot 2016-04-21 at 12

timdorr commented 8 years ago

Async routes are really out of scope of this library. It's main function is for syncing up when using Dev Tools. But you should let Router manage its state in production.

timdorr commented 8 years ago

Dan posted a good writeup on SO today on how you should keep app state and location/router state separate: http://stackoverflow.com/questions/36722584/how-to-sync-redux-state-and-url-hash-tag-params/36749963#36749963