react-router-redux

npm version npm downloads build status

Keep your state in sync with your router :sparkles:

这是一个测试版软件, 它需要:

  1. 一个工作实例
  2. Some people to try it out and find bugs
  3. A strategy for working with the devtools
    • (issue describing a different approach to what we’ve seen previously coming soon)

Installation

  1. npm install --save react-router-redux@next

Usage

Here’s a basic idea of how it works:

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import { createStore, combineReducers, applyMiddleware } from 'redux'
  4. import { Provider } from 'react-redux'
  5. import createHistory from 'history/createBrowserHistory'
  6. import { Route } from 'react-router'
  7. import { ConnectedRouter, routerReducer, routerMiddleware, push } from 'react-router-redux'
  8. import reducers from './reducers' // Or wherever you keep your reducers
  9. // Create a history of your choosing (we're using a browser history in this case)
  10. const history = createHistory()
  11. // Build the middleware for intercepting and dispatching navigation actions
  12. const middleware = routerMiddleware(history)
  13. // Add the reducer to your store on the `router` key
  14. // Also apply our middleware for navigating
  15. const store = createStore(
  16. combineReducers({
  17. ...reducers,
  18. router: routerReducer
  19. }),
  20. applyMiddleware(middleware)
  21. )
  22. // Now you can dispatch navigation actions from anywhere!
  23. // store.dispatch(push('/foo'))
  24. ReactDOM.render(
  25. <Provider store={store}>
  26. { /* ConnectedRouter will use the store from Provider automatically */ }
  27. <ConnectedRouter history={history}>
  28. <div>
  29. <Route exact path="/" component={Home}/>
  30. <Route path="/about" component={About}/>
  31. <Route path="/topics" component={Topics}/>
  32. </div>
  33. </ConnectedRouter>
  34. </Provider>,
  35. document.getElementById('root')
  36. )