Code Monkey home page Code Monkey logo

connected-next-router's Introduction

Connected Next Router

A Redux binding for Next.js Router compatible with Next.js.

Main features

โœจ Keep Router state in sync with your Redux store.

๐ŸŽ‰ Dispatch Router methods (push, replace, go, goBack, goForward, prefetch) using Redux actions.

๐Ÿ•˜ Support time traveling in Redux DevTools.

๐ŸŽ Compatible with next-routes.

๐Ÿ’Ž Ease migration to next.js framework from codebases using connected-react-router or react-router-redux.

Installation

Using npm:

$ npm install --save connected-next-router

Or yarn:

$ yarn add connected-next-router

Usage

Step 1

  • Add routerReducer to your root reducer.
  • Use createRouterMiddleware if you want to dispatch Router actions (ex. to change URL with push('/home')).
  • Use initialRouterState(url) to populate router state in the server side.
...
import { applyMiddleware, compose, createStore, combineReducers } from 'redux'
import { routerReducer, createRouterMiddleware, initialRouterState } from 'connected-next-router'
...
const rootReducer = combineReducers({
  ...reducers,
  router: routerReducer
});

const routerMiddleware = createRouterMiddleware();

// Using next-redux-wrapper's makeStore
export const makeStore = (initialState = {}, options) => {
  if (options.isServer) {
    initialState.router = initialRouterState(options.asPath);
  }

  return createStore(
    rootReducer,
    initialState,
    applyMiddleware(
      routerMiddleware,
      // ... other middlewares ...
    )
  );
}

Step 2

  • Place ConnectedRouter as children of react-redux's Provider.
...
// pages/_app.js
import App, { Container } from 'next/app';
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-next-router'
...
class MyApp extends App {
  render() {
    const { Component, pageProps, store } = this.props;
    return (
      <Container>
        <Provider store={store}>
          <ConnectedRouter>
            <Component { ...pageProps } />
          </ConnectedRouter>
        </Provider>
      </Container>
    );
  }
}

Examples

TODO

Acknowledgements

Acknowledge

License

MIT License

connected-next-router's People

Contributors

danielr18 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.