Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Commit 575d0e9

Browse files
committed
add a basic example
1 parent 972cd49 commit 575d0e9

File tree

16 files changed

+215
-0
lines changed

16 files changed

+215
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ This only depends on the `history.listen` function from react-router
5151
and the `store.getState` and `store.subscribe` functions from redux.
5252
It should be very future-proof with any versions of either libraries.
5353

54+
## Examples
55+
56+
There are examples in the `examples` directory:
57+
58+
1. [basic](https://github.com/jlongster/redux-simple-router/blob/master/examples/basic)
59+
5460
## How to Use
5561

5662
The idea of this library is to use react-router's functionality exactly

examples/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

examples/basic/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This is a basic example that demonstrates rendering components based
2+
on URLs with react-router as well as connecting them to Redux state.
3+
It uses both <Link> elements and the `updatePath` action creator to
4+
update the paths.
5+
6+
To run, follow these steps:
7+
8+
1. Install dependencies with `npm install` (make sure it creates a local node_modules)
9+
2. Build with `webpack --watch`
10+
3. Open `index.html`

examples/basic/actions/count.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const constants = require('../constants');
2+
3+
function increase(n) {
4+
return {
5+
type: constants.INCREASE,
6+
amount: n
7+
};
8+
}
9+
10+
function decrease(n) {
11+
return {
12+
type: constants.DECREASE,
13+
amount: n
14+
};
15+
}
16+
17+
module.exports = { increase, decrease };

examples/basic/app.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const React = require('react');
2+
const ReactDOM = require('react-dom');
3+
const { createStore, combineReducers } = require('redux');
4+
const { Provider } = require('react-redux');
5+
const { Router, Route, IndexRoute } = require('react-router');
6+
const createHistory = require('history/lib/createHashHistory');
7+
const { syncReduxAndRouter, routeReducer } = require('redux-simple-router');
8+
9+
const reducers = require('./reducers');
10+
const { App, Home, Foo, Bar } = require('./components');
11+
12+
const reducer = combineReducers(Object.assign({}, reducers, {
13+
routing: routeReducer
14+
}));
15+
const store = createStore(reducer);
16+
const history = createHistory();
17+
18+
syncReduxAndRouter(history, store);
19+
20+
ReactDOM.render(
21+
<Provider store={store}>
22+
<Router history={history}>
23+
<Route path="/" component={App}>
24+
<IndexRoute component={Home}/>
25+
<Route path="foo" component={Foo}/>
26+
<Route path="bar" component={Bar}/>
27+
</Route>
28+
</Router>
29+
</Provider>,
30+
document.getElementById('mount')
31+
);

examples/basic/components/App.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const React = require('react');
2+
const { Link } = require('react-router');
3+
const { connect } = require('react-redux');
4+
const { updatePath } = require('redux-simple-router');
5+
6+
function App({ updatePath, children }) {
7+
return (
8+
<div>
9+
<header>
10+
Links:
11+
{' '}
12+
<Link to="/">Home</Link>
13+
{' '}
14+
<Link to="/foo">Foo</Link>
15+
{' '}
16+
<Link to="/bar">Bar</Link>
17+
</header>
18+
<div>
19+
<button onClick={() => updatePath('/foo')}>Go to /foo</button>
20+
</div>
21+
<div style={{marginTop: '1.5em'}}>{children}</div>
22+
</div>
23+
);
24+
};
25+
26+
module.exports = connect(
27+
null,
28+
{ updatePath }
29+
)(App);

examples/basic/components/Bar.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const React = require('react');
2+
3+
module.exports = function Bar() {
4+
return <div>And I am Bar!</div>;
5+
}

examples/basic/components/Foo.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const React = require('react');
2+
3+
module.exports = function Foo() {
4+
return <div>I am Foo!</div>;
5+
}

examples/basic/components/Home.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const React = require('react');
2+
const { connect } = require('react-redux');
3+
const { increase, decrease } = require('../actions/count');
4+
5+
function Home({ number, increase, decrease }) {
6+
return (
7+
<div>
8+
Some state changes:
9+
{number}
10+
<button onClick={() => increase(1)}>Increase</button>
11+
<button onClick={() => decrease(1)}>Decrease</button>
12+
</div>
13+
);
14+
};
15+
16+
module.exports = connect(
17+
state => ({ number: state.count.number }),
18+
{ increase, decrease }
19+
)(Home);

examples/basic/components/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const App = require('./App');
2+
const Home = require('./Home');
3+
const Foo = require('./Foo');
4+
const Bar = require('./Bar');
5+
6+
module.exports = { App, Home, Foo, Bar };

examples/basic/constants.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
module.exports = {
3+
INCREASE: 'INCREASE',
4+
DECREASE: 'DECREASE'
5+
}

examples/basic/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>redux-simple-router basic example</title>
5+
<meta charset="utf8"/>
6+
</head>
7+
<body>
8+
<div id="mount"></div>
9+
<script src="dist/bundle.js"></script>
10+
</body>
11+
</html>

examples/basic/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "rsr-basic-example",
3+
"version": "0.0.0",
4+
"dependencies": {
5+
"history": "^1.13.1",
6+
"react": "^0.14.2",
7+
"react-dom": "^0.14.2",
8+
"react-redux": "^4.0.0",
9+
"react-router": "^1.0.0",
10+
"redux": "^3.0.4",
11+
"redux-simple-router": "0.0.8"
12+
},
13+
"devDependencies": {
14+
"babel-core": "^6.1.21",
15+
"babel-loader": "^6.2.0",
16+
"babel-preset-es2015": "^6.1.18",
17+
"babel-preset-react": "^6.1.18",
18+
"webpack": "^1.12.6"
19+
}
20+
}

examples/basic/reducers/count.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const constants = require('../constants');
2+
3+
const initialState = {
4+
number: 1
5+
}
6+
7+
function update(state = initialState, action) {
8+
if(action.type === constants.INCREASE) {
9+
return { number: state.number + action.amount };
10+
}
11+
else if(action.type === constants.DECREASE) {
12+
return { number: state.number - action.amount };
13+
}
14+
return state;
15+
}
16+
17+
module.exports = update;

examples/basic/reducers/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const count = require('./count');
2+
3+
module.exports = { count };

examples/basic/webpack.config.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: './app.js',
5+
output: {
6+
path: path.join(__dirname, 'dist'),
7+
filename: 'bundle.js'
8+
},
9+
module: {
10+
loaders: [{
11+
test: /\.js$/,
12+
loaders: ['babel?presets[]=react,presets[]=es2015'],
13+
exclude: /node_modules/,
14+
include: __dirname
15+
}]
16+
}
17+
}
18+
19+
20+
21+
// This will make the redux-simpler-router module resolve to the
22+
// latest src instead of using it from npm. Remove this if running
23+
// outside of the source.
24+
var src = path.join(__dirname, '..', '..', 'src')
25+
var fs = require('fs')
26+
if (fs.existsSync(src)) {
27+
// Use the latest src
28+
module.exports.resolve = { alias: { 'redux-simple-router': src } }
29+
}

0 commit comments

Comments
 (0)