├── .babelrc ├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── examples ├── .gitignore ├── basic │ ├── .babelrc │ ├── README.md │ ├── actions │ │ └── count.js │ ├── app.js │ ├── components │ │ ├── App.js │ │ ├── Bar.js │ │ ├── Foo.js │ │ ├── Home.js │ │ └── index.js │ ├── constants.js │ ├── index.html │ ├── package.json │ ├── reducers │ │ ├── count.js │ │ └── index.js │ └── webpack.config.js └── server │ ├── .babelrc │ ├── client.js │ ├── index.html │ ├── package.json │ ├── routes.js │ ├── server.js │ ├── store.js │ └── webpack.config.js ├── karma.conf.js ├── package.json ├── src ├── actions.js ├── index.js ├── middleware.js ├── reducer.js └── sync.js ├── test ├── .eslintrc ├── _createSyncTest.js ├── actions.spec.js ├── browser │ └── index.js ├── middleware.spec.js ├── reducer.spec.js └── sync.spec.js ├── tests.webpack.js ├── webpack.config.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "react", "stage-1"], 3 | "plugins": [ 4 | "transform-es3-member-expression-literals", 5 | "transform-es3-property-literals" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | insert_final_newline = true 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | 10 | [*.md] 11 | trim_trailing_whitespace = false 12 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | examples/*/node_modules/ 2 | examples/*/dist/ 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "react-app" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | dist 3 | node_modules 4 | coverage 5 | *.log 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | branches: 3 | only: 4 | - master 5 | language: node_js 6 | cache: 7 | directories: 8 | - node_modules 9 | node_js: 10 | - "4" 11 | - "6" 12 | before_script: 13 | - export DISPLAY=:99.0 14 | - sh -e /etc/init.d/xvfb start 15 | script: 16 | - npm run test 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [4.0.8](https://github.com/reactjs/react-router-redux/compare/v4.0.7...v4.0.8) 2 | 3 | - Don't run listeners synchronously with history 3 [`b2c2259`](https://github.com/reactjs/react-router-redux/commit/b2c2259c189cafad3e6e68eac7729e74f2bd56a9) 4 | 5 | ## [4.0.7](https://github.com/reactjs/react-router-redux/compare/v4.0.6...v4.0.7) 6 | 7 | - Support history 3 [#476](https://github.com/reactjs/react-router-redux/pull/476) 8 | 9 | ## [4.0.6](https://github.com/reactjs/react-router-redux/compare/v4.0.5...v4.0.6) 10 | 11 | - Makes sure the state in the store matches the state in history when using SSR [#445](https://github.com/reactjs/react-router-redux/pull/445) 12 | 13 | ## [4.0.5](https://github.com/reactjs/react-router-redux/compare/v4.0.4...v4.0.5) 14 | 15 | - Initialize currentLocation to initial location from the store. [#403](https://github.com/reactjs/react-router-redux/pull/403) 16 | 17 | ## [4.0.4](https://github.com/reactjs/react-router-redux/compare/v4.0.2...v4.0.4) 18 | 19 | - Added a UMD build. [#362](https://github.com/reactjs/react-router-redux/pull/362) 20 | 21 | ## [4.0.2](https://github.com/reactjs/react-router-redux/compare/v4.0.1...v4.0.2) 22 | 23 | - Calling routerReducer() with no args crashes. [#350](https://github.com/reactjs/react-router-redux/pull/350) 24 | 25 | ## [4.0.1](https://github.com/reactjs/react-router-redux/compare/v4.0.0...v4.0.1) 26 | 27 | - Fix IE8 compatbility. [#344](https://github.com/reactjs/react-router-redux/pull/344) 28 | 29 | ## [4.0.0](https://github.com/reactjs/react-router-redux/compare/3.0.0...v4.0.0) 30 | 31 | This is a big breaking release, but the last one for the foreseeable future. The scope of this library has changed, so please re-evaluate its usefulness to you. You may not need it and this is ok! 32 | 33 | #### Summary of Changes 34 | 35 | The provided action creators and middleware are now separate from the history<->state syncing function. For the vast majority of cases, using action creators to trigger navigation is obsoleted by React Router's [new history singletons](https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#history-singletons-provided) provided in 2.0. Building this functionality in by default and coupling it to our history syncing logic no longer makes sense. 36 | 37 | We now sync by enhancing the history instance to listen for navigation events and dispatch those into the store. The enhanced history has its `listen` method overridden to respond to store changes, rather than directly to navigation events. When this history is provided to ``, the router will listen to it and receive these store changes. This means if we time travel with the store, the router will receive those store changes and update based on the location in the store, instead of what the browser says. Normal navigation events (hitting your browser back/forward buttons, telling a history singleton to `push` a location) flow through the history's listener like normal, so all the usual stuff works A-OK. 38 | 39 | ## [3.0.0](https://github.com/reactjs/react-router-redux/compare/2.1.0...3.0.0) 40 | 41 | Technically, 2.1.0 broke semver. The appropriate @timdorr's have been flogged. So, we're bumping the major version to catch up. 42 | 43 | - Fixed Resets in Redux Dev Tools. [3ae8110f](https://github.com/reactjs/react-router-redux/commit/3ae8110f) 44 | - Ensure the initialState is set properly. [a00acfd4](https://github.com/reactjs/react-router-redux/commit/a00acfd4) 45 | - Support any number of args on action creators [524898b5](https://github.com/reactjs/react-router-redux/commit/524898b5) 46 | 47 | ## [2.1.0](https://github.com/reactjs/react-router-redux/compare/2.0.4...2.1.0) 48 | 49 | - `listenForReplays` has a `selectLocationState` selector. [#218](https://github.com/reactjs/react-router-redux/pull/218) 50 | - Provide unscoped action creators. [#225](https://github.com/reactjs/react-router-redux/pull/225) 51 | - Example updated to use fully ES2015 syntax. 52 | 53 | ## [2.0.4](https://github.com/reactjs/react-router-redux/compare/2.0.2...2.0.4) 54 | 55 | - Remove `history` module published within the tarball. [#133](https://github.com/reactjs/react-router-redux/issues/133) 56 | - Make actions conform to [Flux Standard Action](https://github.com/acdlite/flux-standard-action). [#208](https://github.com/reactjs/react-router-redux/pull/208) 57 | 58 | ## [2.0.2](https://github.com/reactjs/react-router-redux/compare/1.0.2...2.0.2) 59 | 60 | Versions 2.0.0 and 2.0.1 were test releases for the 2.* series. 2.0.2 is the first public release. 61 | 62 | **A whole new API, with many breaking changes:** 63 | 64 | * `syncReduxAndRouter` is gone. Instead, call `syncHistory` with just the `history` object, which returns a middleware that you need to apply. (#141) 65 | * If you use redux devtools, you need to call `middleware.listenForReplays(store)` on the middleware returned from `syncHistory`. Create the store first with the middleware, then call this function with the store. 66 | * Action creators are now contained in a single object called `routeActions`. `go`, `goBack`, and `goForward` action creators have been added. 67 | * `UPDATE_PATH` is now `UPDATE_LOCATION`. 68 | * The fully parsed [location object](https://github.com/reactjs/history/blob/master/docs/Location.md) is now stored in the state instead of a URL string. To access the path, use `state.routing.location.pathname` instead of `state.routing.path`. 69 | 70 | [View the new docs](https://github.com/reactjs/react-router-redux#api) 71 | 72 | ## [1.0.2](https://github.com/reactjs/react-router-redux/compare/1.0.1...1.0.2) 73 | 74 | * Only publish relevant files to npm 75 | 76 | ## [1.0.1](https://github.com/reactjs/react-router-redux/compare/1.0.0...1.0.1) 77 | 78 | * Solve problem with `basename` causing infinite redirects (#103) 79 | * Switched to ES6 imports/exports internally, but should not affect outside users 80 | 81 | ## [1.0.0](https://github.com/reactjs/react-router-redux/compare/0.0.10...1.0.0) 82 | > 2015-12-09 83 | 84 | This release changes quite a bit so you'll have to update your code. 85 | 86 | **Breaking Changes:** 87 | 88 | * The `updatePath` action creator has been removed in favor of `pushPath` and `replacePath`. Use `pushPath` to get the same behavior as before. (#38) 89 | * We have added support for routing state (#38) 90 | * Our actions are now [FSA compliant](https://github.com/acdlite/flux-standard-action). This means if you are listening for the `UPDATE_PATH` action in a reducer you should get properties off the `payload` property. (#63) 91 | 92 | Other fixes: 93 | 94 | * Redux DevTools should now work as expected (#73) 95 | * As we no longer depend on `window.location`, `` should now work (#62) 96 | * We've done lots of work on finding the right way to stop cycles, so hopefully we shouldn't have any unnecessary location or store updates (#50) 97 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015-present James Long 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Project Deprecated 2 | 3 | This project is no longer maintained. For your Redux <-> Router syncing needs with React Router 4+, please see one of these libraries instead: 4 | 5 | * [connected-react-router](https://github.com/supasate/connected-react-router) 6 | 7 | --- 8 | 9 | ⚠️ **This repo is for react-router-redux 4.x, which is only compatible with react-router 2.x and 3.x** 10 | 11 | # react-router-redux 12 | 13 | [![npm version](https://img.shields.io/npm/v/react-router-redux.svg?style=flat-square)](https://www.npmjs.com/package/react-router-redux) [![npm downloads](https://img.shields.io/npm/dm/react-router-redux.svg?style=flat-square)](https://www.npmjs.com/package/react-router-redux) [![build status](https://img.shields.io/travis/reactjs/react-router-redux/master.svg?style=flat-square)](https://travis-ci.org/reactjs/react-router-redux) 14 | 15 | > **Keep your router in sync with application state** :sparkles: 16 | 17 | _Formerly known as redux-simple-router_ 18 | 19 | You're a smart person. You use [Redux](https://github.com/reactjs/redux) to manage your application state. You use [React Router](https://github.com/reactjs/react-router) to do routing. All is good. 20 | 21 | But the two libraries don't coordinate. You want to do time travel with your application state, but React Router doesn't navigate between pages when you replay actions. It controls an important part of application state: the URL. 22 | 23 | This library helps you keep that bit of state in sync with your Redux store. We keep a copy of the current location hidden in state. When you rewind your application state with a tool like [Redux DevTools](https://github.com/gaearon/redux-devtools), that state change is propagated to React Router so it can adjust the component tree accordingly. You can jump around in state, rewinding, replaying, and resetting as much as you'd like, and this library will ensure the two stay in sync at all times. 24 | 25 | **This library is not _necessary_ for using Redux together with React Router. You can use the two together just fine without any additional libraries. It is useful if you care about recording, persisting, and replaying user actions, using time travel. If you don't care about these features, just [use Redux and React Router directly](http://stackoverflow.com/questions/36722584/how-to-sync-redux-state-and-url-hash-tag-params/36749963#36749963).** 26 | 27 | ## Installation 28 | 29 | ``` 30 | npm install --save react-router-redux 31 | ``` 32 | 33 | ## How It Works 34 | 35 | This library allows you to use React Router's APIs as they are documented. And, you can use redux like you normally would, with a single app state. The library simply enhances a history instance to allow it to synchronize any changes it receives into application state. 36 | 37 | [history](https://github.com/reactjs/history) + `store` ([redux](https://github.com/reactjs/redux)) → [**react-router-redux**](https://github.com/reactjs/react-router-redux) → enhanced [history](https://github.com/reactjs/history) → [react-router](https://github.com/reactjs/react-router) 38 | 39 | ## Tutorial 40 | 41 | Let's take a look at a simple example. 42 | 43 | ```js 44 | import React from 'react' 45 | import ReactDOM from 'react-dom' 46 | import { createStore, combineReducers } from 'redux' 47 | import { Provider } from 'react-redux' 48 | import { Router, Route, browserHistory } from 'react-router' 49 | import { syncHistoryWithStore, routerReducer } from 'react-router-redux' 50 | 51 | import reducers from '/reducers' 52 | 53 | // Add the reducer to your store on the `routing` key 54 | const store = createStore( 55 | combineReducers({ 56 | ...reducers, 57 | routing: routerReducer 58 | }) 59 | ) 60 | 61 | // Create an enhanced history that syncs navigation events with the store 62 | const history = syncHistoryWithStore(browserHistory, store) 63 | 64 | ReactDOM.render( 65 | 66 | { /* Tell the Router to use our enhanced history */ } 67 | 68 | 69 | 70 | 71 | 72 | 73 | , 74 | document.getElementById('mount') 75 | ) 76 | ``` 77 | 78 | Now any time you navigate, which can come from pressing browser buttons or navigating in your application code, the enhanced history will first pass the new location through the Redux store and then on to React Router to update the component tree. If you time travel, it will also pass the new state to React Router to update the component tree again. 79 | 80 | #### How do I watch for navigation events, such as for analytics? 81 | 82 | Simply listen to the enhanced history via `history.listen`. This takes in a function that will receive a `location` any time the store updates. This includes any time travel activity performed on the store. 83 | 84 | ```js 85 | const history = syncHistoryWithStore(browserHistory, store) 86 | 87 | history.listen(location => analyticsService.track(location.pathname)) 88 | ``` 89 | 90 | For other kinds of events in your system, you can use middleware on your Redux store like normal to watch any action that is dispatched to the store. 91 | 92 | #### What if I use Immutable.js or another state wrapper with my Redux store? 93 | 94 | When using a wrapper for your store's state, such as Immutable.js, you will need to change two things from the standard setup: 95 | 96 | 1. By default, the library expects to find the history state at `state.routing`. If your wrapper prevents accessing properties directly, or you want to put the routing state elsewhere, pass a selector function to access the historystate via the `selectLocationState` option on `syncHistoryWithStore`. 97 | 2. Provide your own reducer function that will receive actions of type `LOCATION_CHANGE` and return the payload merged into the `locationBeforeTransitions` property of the routing state. For example, `state.set("routing", {locationBeforeTransitions: action.payload})`. 98 | 99 | These two hooks will allow you to store the state that this library uses in whatever format or wrapper you would like. 100 | 101 | #### How do I access router state in a container component? 102 | 103 | React Router [provides route information via a route component's props](https://github.com/ReactTraining/react-router/blob/v3/docs/Introduction.md#getting-url-parameters). This makes it easy to access them from a container component. When using [react-redux](https://github.com/reactjs/react-redux) to `connect()` your components to state, you can access the router's props from the [2nd argument of `mapStateToProps`](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options): 104 | 105 | ```js 106 | function mapStateToProps(state, ownProps) { 107 | return { 108 | id: ownProps.params.id, 109 | filter: ownProps.location.query.filter 110 | }; 111 | } 112 | ``` 113 | 114 | You should not read the location state directly from the Redux store. This is because React Router operates asynchronously (to handle things such as dynamically-loaded components) and your component tree may not yet be updated in sync with your Redux state. You should rely on the props passed by React Router, as they are only updated after it has processed all asynchronous code. 115 | 116 | #### What if I want to issue navigation events via Redux actions? 117 | 118 | React Router provides singleton versions of history (`browserHistory` and `hashHistory`) that you can import and use from anywhere in your application. However, if you prefer Redux style actions, the library also provides a set of action creators and a middleware to capture them and redirect them to your history instance. 119 | 120 | ```js 121 | import { createStore, combineReducers, applyMiddleware } from 'redux'; 122 | import { routerMiddleware, push } from 'react-router-redux' 123 | 124 | // Apply the middleware to the store 125 | const middleware = routerMiddleware(browserHistory) 126 | const store = createStore( 127 | reducers, 128 | applyMiddleware(middleware) 129 | ) 130 | 131 | // Dispatch from anywhere like normal. 132 | store.dispatch(push('/foo')) 133 | ``` 134 | 135 | ## Examples 136 | 137 | - [examples/basic](/examples/basic) - basic reference implementation 138 | 139 | Examples from the community: 140 | 141 | * [react-redux-styled-hot-universal](https://github.com/krasevych/react-redux-styled-hot-universal) (SSR, Universal Webpack, Redux, React-router, Webpack 2, Babel, Styled Components and more...) 142 | * [shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) - react-router-redux including **Server Rendering** using [React on Rails](https://github.com/shakacode/react_on_rails/), live at [www.reactrails.com](http://www.reactrails.com/). 143 | * [davezuko/react-redux-starter-kit](https://github.com/davezuko/react-redux-starter-kit) - popular redux starter kit 144 | * **tip**: migrating from react-router-redux `^3.0.0`? use [this commit](https://github.com/davezuko/react-redux-starter-kit/commit/0df26907) as a reference 145 | * [svrcekmichal/universal-react](https://github.com/svrcekmichal/universal-react) - Universal react app with async actions provided by [svrcekmichal/reasync](https://github.com/svrcekmichal/reasync) package 146 | * [steveniseki/react-router-redux-example](https://github.com/StevenIseki/react-router-redux-example) - minimal react-router-redux example includes css modules and universal rendering 147 | * [choonkending/react-webpack-node](https://github.com/choonkending/react-webpack-node) - Full-stack universal Redux App 148 | * [kuy/treemap-with-router](https://github.com/kuy/treemap-with-router) - An example for react-router-redux with d3's treemap. 149 | 150 | → _Have an example to add? Send us a PR!_ ← 151 | 152 | ## API 153 | 154 | #### `routerReducer()` 155 | 156 | **You must add this reducer to your store for syncing to work.** 157 | 158 | A reducer function that stores location updates from `history`. If you use `combineReducers`, it should be nested under the `routing` key. 159 | 160 | #### `history = syncHistoryWithStore(history, store, [options])` 161 | 162 | Creates an enhanced history from the provided history. This history changes `history.listen` to pass all location updates through the provided store first. This ensures if the store is updated either from a navigation event or from a time travel action, such as a replay, the listeners of the enhanced history will stay in sync. 163 | 164 | **You must provide the enhanced history to your `` component.** This ensures your routes stay in sync with your location and your store at the same time. 165 | 166 | The `options` object takes in the following optional keys: 167 | 168 | - `selectLocationState` - (default `state => state.routing`) A selector function to obtain the history state from your store. Useful when not using the provided `routerReducer` to store history state. Allows you to use wrappers, such as Immutable.js. 169 | - `adjustUrlOnReplay` - (default `true`) When `false`, the URL will not be kept in sync during time travel. This is useful when using `persistState` from Redux DevTools and not wanting to maintain the URL state when restoring state. 170 | 171 | #### `push(location)`, `replace(location)`, `go(number)`, `goBack()`, `goForward()` 172 | 173 | **You must install `routerMiddleware` for these action creators to work.** 174 | 175 | Action creators that correspond with the [history methods of the same name](https://github.com/ReactTraining/history/blob/v3/docs/GettingStarted.md#navigation). For reference they are defined as follows: 176 | 177 | - `push` - Pushes a new location to history, becoming the current location. 178 | - `replace` - Replaces the current location in history. 179 | - `go` - Moves backwards or forwards a relative number of locations in history. 180 | - `goForward` - Moves forward one location. Equivalent to `go(1)` 181 | - `goBack` - Moves backwards one location. Equivalent to `go(-1)` 182 | 183 | Both `push` and `replace` take in a [location descriptor](https://github.com/ReactTraining/history/blob/v3/docs/Location.md), which can be an object describing the URL or a plain string URL. 184 | 185 | These action creators are also available in one single object as `routerActions`, which can be used as a convenience when using Redux's `bindActionCreators()`. 186 | 187 | #### `routerMiddleware(history)` 188 | 189 | A middleware you can apply to your Redux `store` to capture dispatched actions created by the action creators. It will redirect those actions to the provided `history` instance. 190 | 191 | #### `LOCATION_CHANGE` 192 | 193 | An action type that you can listen for in your reducers to be notified of route updates. Fires *after* any changes to history. 194 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | *.log 4 | -------------------------------------------------------------------------------- /examples/basic/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "react", "stage-1"] 3 | } 4 | -------------------------------------------------------------------------------- /examples/basic/README.md: -------------------------------------------------------------------------------- 1 | react-router-redux basic example 2 | ================================= 3 | 4 | This is a basic example that demonstrates rendering components based 5 | on URLs with `react-router` as well as connecting them to Redux state. 6 | It uses both `` elements as well as the `push` action creator 7 | provided by react-router-redux. 8 | 9 | This example also demonstrates integration with 10 | **[redux-devtools](https://github.com/gaearon/redux-devtools) ^3.0.0** 11 | 12 | **To run, follow these steps:** 13 | 14 | 1. Install dependencies with `npm install` in this directory (make sure it creates a local node_modules) 15 | 2. By default, it uses the local version from `src` of react-router-redux, so you need to run `npm install` from there first. If you want to use a version straight from npm, remove the lines in `webpack.config.js` at the bottom. 16 | 3. Start build with `npm start` 17 | 4. Open [http://localhost:8080/](http://localhost:8080/) 18 | 19 | - 20 | 21 | If you want to run the example from the npm published version of 22 | **react-router-redux**, remove the alias in `webpack.config` 23 | to the source from line 21. 24 | 25 | This example uses the latest version, switch to a specific tag to use a stable version: 26 | 27 | e.g. [react-router-redux tag 1.0.2](https://github.com/reactjs/react-router-redux/tree/1.0.2/examples/basic) 28 | -------------------------------------------------------------------------------- /examples/basic/actions/count.js: -------------------------------------------------------------------------------- 1 | import { INCREASE, DECREASE } from '../constants' 2 | 3 | export function increase(n) { 4 | return { 5 | type: INCREASE, 6 | amount: n 7 | } 8 | } 9 | 10 | export function decrease(n) { 11 | return { 12 | type: DECREASE, 13 | amount: n 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /examples/basic/app.js: -------------------------------------------------------------------------------- 1 | import { createDevTools } from 'redux-devtools' 2 | import LogMonitor from 'redux-devtools-log-monitor' 3 | import DockMonitor from 'redux-devtools-dock-monitor' 4 | 5 | import React from 'react' 6 | import ReactDOM from 'react-dom' 7 | import { createStore, combineReducers } from 'redux' 8 | import { Provider } from 'react-redux' 9 | import { Router, Route, IndexRoute, browserHistory } from 'react-router' 10 | import { syncHistoryWithStore, routerReducer } from 'react-router-redux' 11 | 12 | import * as reducers from './reducers' 13 | import { App, Home, Foo, Bar } from './components' 14 | 15 | const reducer = combineReducers({ 16 | ...reducers, 17 | routing: routerReducer 18 | }) 19 | 20 | const DevTools = createDevTools( 21 | 22 | 23 | 24 | ) 25 | 26 | const store = createStore( 27 | reducer, 28 | DevTools.instrument() 29 | ) 30 | const history = syncHistoryWithStore(browserHistory, store) 31 | 32 | ReactDOM.render( 33 | 34 |
35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 |
44 |
, 45 | document.getElementById('mount') 46 | ) 47 | -------------------------------------------------------------------------------- /examples/basic/components/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Link, browserHistory } from 'react-router' 3 | 4 | export default function App({ children }) { 5 | return ( 6 |
7 |
8 | Links: 9 | {' '} 10 | Home 11 | {' '} 12 | Foo 13 | {' '} 14 | Bar 15 |
16 |
17 | 18 |
19 |
{children}
20 |
21 | ) 22 | } 23 | -------------------------------------------------------------------------------- /examples/basic/components/Bar.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export default function Bar() { 4 | return
And I am Bar!
5 | } 6 | -------------------------------------------------------------------------------- /examples/basic/components/Foo.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export default function Foo() { 4 | return
I am Foo!
5 | } 6 | -------------------------------------------------------------------------------- /examples/basic/components/Home.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { connect } from 'react-redux' 3 | import { increase, decrease } from '../actions/count' 4 | 5 | function Home({ number, increase, decrease }) { 6 | return ( 7 |
8 | Some state changes: 9 | {number} 10 | 11 | 12 |
13 | ) 14 | } 15 | 16 | export default connect( 17 | state => ({ number: state.count.number }), 18 | { increase, decrease } 19 | )(Home) 20 | -------------------------------------------------------------------------------- /examples/basic/components/index.js: -------------------------------------------------------------------------------- 1 | export App from './App' 2 | export Home from './Home' 3 | export Foo from './Foo' 4 | export Bar from './Bar' 5 | -------------------------------------------------------------------------------- /examples/basic/constants.js: -------------------------------------------------------------------------------- 1 | 2 | export const INCREASE = 'INCREASE' 3 | export const DECREASE = 'DECREASE' 4 | -------------------------------------------------------------------------------- /examples/basic/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | react-router-redux basic example 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/basic/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rrr-basic-example", 3 | "version": "0.0.0", 4 | "repository": "reactjs/react-router-redux", 5 | "license": "MIT", 6 | "dependencies": { 7 | "react": "^0.14.7", 8 | "react-dom": "^0.14.7", 9 | "react-redux": "^4.3.0", 10 | "react-router": "^2.0.0", 11 | "redux": "^3.2.1", 12 | "react-router-redux": "^4.0.0" 13 | }, 14 | "devDependencies": { 15 | "babel-core": "^6.4.5", 16 | "babel-eslint": "^5.0.0-beta9", 17 | "babel-loader": "^6.2.2", 18 | "babel-preset-es2015": "^6.3.13", 19 | "babel-preset-react": "^6.3.13", 20 | "babel-preset-stage-1": "^6.3.13", 21 | "eslint": "^1.10.3", 22 | "eslint-config-rackt": "^1.1.1", 23 | "eslint-plugin-react": "^3.16.1", 24 | "redux-devtools": "^3.1.0", 25 | "redux-devtools-dock-monitor": "^1.0.1", 26 | "redux-devtools-log-monitor": "^1.0.4", 27 | "webpack": "^1.12.13", 28 | "webpack-dev-server": "^1.14.1" 29 | }, 30 | "scripts": { 31 | "start": "webpack-dev-server --history-api-fallback --no-info --open" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /examples/basic/reducers/count.js: -------------------------------------------------------------------------------- 1 | import { INCREASE, DECREASE } from '../constants' 2 | 3 | const initialState = { 4 | number: 1 5 | } 6 | 7 | export default function update(state = initialState, action) { 8 | if(action.type === INCREASE) { 9 | return { number: state.number + action.amount } 10 | } 11 | else if(action.type === DECREASE) { 12 | return { number: state.number - action.amount } 13 | } 14 | return state 15 | } 16 | -------------------------------------------------------------------------------- /examples/basic/reducers/index.js: -------------------------------------------------------------------------------- 1 | export count from './count' 2 | -------------------------------------------------------------------------------- /examples/basic/webpack.config.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | const path = require('path'); 3 | 4 | module.exports = { 5 | entry: './app.js', 6 | output: { 7 | path: path.join(__dirname, 'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | module: { 11 | loaders: [{ 12 | test: /\.js$/, 13 | loader: 'babel', 14 | exclude: /node_modules/, 15 | include: __dirname 16 | }] 17 | } 18 | } 19 | 20 | 21 | 22 | // This will make the redux-simpler-router module resolve to the 23 | // latest src instead of using it from npm. Remove this if running 24 | // outside of the source. 25 | var src = path.join(__dirname, '..', '..', 'src') 26 | var fs = require('fs') 27 | if (fs.existsSync(src)) { 28 | // Use the latest src 29 | module.exports.resolve = { alias: { 'react-router-redux': src } } 30 | module.exports.module.loaders.push({ 31 | test: /\.js$/, 32 | loaders: ['babel'], 33 | include: src 34 | }); 35 | } 36 | -------------------------------------------------------------------------------- /examples/server/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "react", "stage-1"], 3 | "plugins": [ 4 | ["babel-plugin-module-alias", [ 5 | { "src": "../../src", "expose": "react-router-redux" } 6 | ]] 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /examples/server/client.js: -------------------------------------------------------------------------------- 1 | import 'babel-polyfill' 2 | 3 | import React from 'react' 4 | import { render } from 'react-dom' 5 | 6 | import { Provider } from 'react-redux' 7 | import { Router, browserHistory } from 'react-router' 8 | import { syncHistoryWithStore } from 'react-router-redux' 9 | 10 | import { configureStore, DevTools } from './store' 11 | import routes from './routes' 12 | 13 | const store = configureStore(browserHistory, window.__initialState__) 14 | const history = syncHistoryWithStore(browserHistory, store) 15 | 16 | render( 17 | 18 | 19 | , 20 | document.getElementById('root') 21 | ) 22 | 23 | render( 24 | 25 | 26 | , 27 | document.getElementById('devtools') 28 | ) 29 | -------------------------------------------------------------------------------- /examples/server/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | react-router-redux server rendering example 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rrr-server-example", 3 | "version": "0.0.0", 4 | "repository": "reactjs/react-router-redux", 5 | "license": "MIT", 6 | "dependencies": { 7 | "react": "^0.14.7", 8 | "react-dom": "^0.14.7", 9 | "react-redux": "^4.3.0", 10 | "react-router": "^2.0.0", 11 | "react-router-redux": "^4.0.0", 12 | "redux": "^3.2.1", 13 | "serialize-javascript": "^1.1.2" 14 | }, 15 | "devDependencies": { 16 | "babel-cli": "^6.5.1", 17 | "babel-core": "^6.4.5", 18 | "babel-eslint": "^5.0.0-beta9", 19 | "babel-loader": "^6.2.2", 20 | "babel-plugin-module-alias": "^1.2.0", 21 | "babel-preset-es2015": "^6.3.13", 22 | "babel-preset-react": "^6.3.13", 23 | "babel-preset-stage-1": "^6.3.13", 24 | "babel-register": "^6.5.2", 25 | "eslint": "^1.10.3", 26 | "eslint-config-rackt": "^1.1.1", 27 | "eslint-plugin-react": "^3.16.1", 28 | "express": "^4.13.4", 29 | "redux-devtools": "^3.1.1", 30 | "redux-devtools-dock-monitor": "^1.0.1", 31 | "redux-devtools-log-monitor": "^1.0.4", 32 | "webpack": "^1.12.13", 33 | "webpack-dev-middleware": "^1.5.1" 34 | }, 35 | "scripts": { 36 | "start": "babel-node server.js" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /examples/server/routes.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Route, IndexRoute, Link } from 'react-router' 3 | 4 | const App = ({ children }) => ( 5 |
6 |
7 | Links: 8 | {' '} 9 | Home 10 | {' '} 11 | Foo 12 | {' '} 13 | Bar 14 |
15 | {children} 16 |
17 | ) 18 | 19 | const Home = () => (
Home!
) 20 | const Foo = () => (
Foo!
) 21 | const Bar = () => (
Bar!
) 22 | 23 | const routes = ( 24 | 25 | 26 | 27 | 28 | 29 | ) 30 | 31 | export default routes 32 | -------------------------------------------------------------------------------- /examples/server/server.js: -------------------------------------------------------------------------------- 1 | /*eslint-disable no-console */ 2 | import express from 'express' 3 | import serialize from 'serialize-javascript' 4 | 5 | import webpack from 'webpack' 6 | import webpackDevMiddleware from 'webpack-dev-middleware' 7 | import webpackConfig from './webpack.config' 8 | 9 | import React from 'react' 10 | import { renderToString } from 'react-dom/server' 11 | import { Provider } from 'react-redux' 12 | import { createMemoryHistory, match, RouterContext } from 'react-router' 13 | import { syncHistoryWithStore } from '../../src' 14 | 15 | import { configureStore } from './store' 16 | import routes from './routes' 17 | 18 | const app = express() 19 | 20 | app.use(webpackDevMiddleware(webpack(webpackConfig), { 21 | publicPath: '/__build__/', 22 | stats: { 23 | colors: true 24 | } 25 | })) 26 | 27 | const HTML = ({ content, store }) => ( 28 | 29 | 30 |
31 |
32 |