├── .babelrc
├── .gitignore
├── .npmignore
├── README.md
├── examples
├── README.md
├── global.css
├── index.html
├── passing-props-to-children
│ ├── app.css
│ ├── app.js
│ └── index.html
├── server.js
├── transitions
│ ├── app.js
│ └── index.html
└── webpack.config.js
├── index.js
├── package.json
└── src
├── connectHistory.js
├── connectLifecycle.js
├── connectRouteContext.js
└── index.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["es2015", "react", "stage-2"]
3 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | lib
2 | node_modules
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | src
2 | .babelrc
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-router-ad-hocs
2 |
3 | This is a set of three higher-order components that do (more-or-less) the same things as react-router's mixins. You can use these with React components that you've defined as ES6 classes or with those fancy, stateless, functional components that all the kids are talking about.
4 |
5 | Here's how they work:
6 |
7 | ```javascript
8 | import React, { Component } from 'react'
9 | import { connectHistory } from 'react-router-ad-hocs'
10 |
11 | class MySpecialLink extends Component {
12 | handleClick () {
13 | this.props.history.pushState(null, '/foo')
14 | }
15 | render () {
16 | return (
17 |
this.handleClick()}>
18 | Click Me!
19 |
20 | )
21 | }
22 | }
23 |
24 | export default connectHistory(MySpecialLink)
25 | ```
26 | ## API
27 |
28 | ### `connectHistory(ComponentToBeWrapped)`
29 |
30 | #### Parameters:
31 | ComponentToBeWrapped: Any React component
32 | #### Returns:
33 | A wrapped version of ComponentToBeWrapped that will receive react-router's `history` object as a prop. See the [History mixin](https://github.com/rackt/react-router/blob/master/docs/API.md#history-mixin) documentation for more information. If you are already passing a prop called `history` yourself, it will be overwritten by `connectHistory()`, so don't do that.
34 |
35 | ### `connectLifecycle(ComponentToBeWrapped)`
36 |
37 | #### Parameters:
38 | ComponentToBeWrapped: Any React component that defines a `routerWillLeave` method
39 | #### Returns:
40 | A wrapped version of ComponentToBeWrapped. ComponentToBeWrapped's `routerWillLeave` method will be called immediately before the router leaves the current route, with the ability to cancel the navigation. See the [Lifecycle mixin](https://github.com/rackt/react-router/blob/master/docs/API.md#lifecycle-mixin) documentation for more information.
41 |
42 | ### `connectRouteContext(ComponentToBeWrapped)`
43 | #### Parameters:
44 | ComponentToBeWrapped: Any React component that receives `route` as a prop (i.e. a Route component)
45 |
46 | #### Returns:
47 | A wrapped version of ComponentToBeWrapped that provides `route` on context for all its children. You'd use this on Route components whose children want to use `connectLifecycle()` because `connectLifecycle()` needs to know about the current `route`. See the [RouteContext mixin](https://github.com/rackt/react-router/blob/master/docs/API.md#routecontext-mixin) documentation for more information.
48 |
49 | ## Caveats and differences from react-router mixins:
50 |
51 | 1. `connectHistory()` passes history as a prop, whereas the `History` mixin adds it directly to your component definition and makes it accessible through `this.history`. I decided to pass it as a prop because props are more easily composable. It's probably not a big deal either way, so I guess let me know if this is a problem for you.
52 |
53 | 2. If you're using `connectHistory()` and `connectLifecycle()` together on the same component, it's important that `connectLifecycle()` be applied first (i.e. receive the component first in the composition chain) because it needs to directly access the unwrapped component's `.routerWillLeave()` method, which will otherwise be obscured by `connectHistory()`. So do this:
54 |
55 | ```javascript
56 | const WrappedComponent = connectHistory(connectLifecycle(UnwrappedComponent))
57 | ```
58 |
59 | not this:
60 |
61 | ```javascript
62 | const WrappedComponent = connectLifecycle(connectHistory(UnwrappedComponent))
63 | ```
64 |
65 | That should cover everything. Have fun!
66 |
--------------------------------------------------------------------------------
/examples/README.md:
--------------------------------------------------------------------------------
1 | React Router Examples
2 | =====================
3 |
4 | To run the examples in your development environment:
5 |
6 | 1. Clone this repo
7 | 2. Run `npm install`
8 | 3. Start the development server with `npm start`
9 | 4. Point your browser to http://localhost:8080
10 |
--------------------------------------------------------------------------------
/examples/global.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: "Helvetica Neue", Arial;
3 | font-weight: 200;
4 | }
5 |
6 | h1, h2, h3 {
7 | font-weight: 100;
8 | }
9 |
10 | a {
11 | color: hsl(200, 50%, 50%);
12 | }
13 |
14 | a.active {
15 | color: hsl(20, 50%, 50%);
16 | }
17 |
18 | .breadcrumbs a {
19 | text-decoration: none;
20 | }
21 |
--------------------------------------------------------------------------------
/examples/index.html:
--------------------------------------------------------------------------------
1 |
2 | React Router Examples
3 |
4 |
5 |