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

add a basic example #35

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
17 changes: 17 additions & 0 deletions examples/basic/actions/count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const constants = require('../constants');

function increase(n) {
return {
type: constants.INCREASE,
amount: n
};
}

function decrease(n) {
return {
type: constants.DECREASE,
amount: n
};
}

module.exports = { increase, decrease };
30 changes: 30 additions & 0 deletions examples/basic/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const React = require('react');
const ReactDOM = require('react-dom');
const { createStore, combineReducers } = require('redux');
const { Provider } = require('react-redux');
const { Router, Route } = require('react-router');
const createHistory = require('history/lib/createHashHistory');
const { syncReduxAndRouter, routeReducer } = require('redux-simple-router');

const reducers = require('./reducers');
const { App, Foo, Bar } = require('./components');

const reducer = combineReducers(Object.assign({}, reducers, {
routing: routeReducer
}));
const store = createStore(reducer);
const history = createHistory();

syncReduxAndRouter(history, store);

ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<Route path="foo" component={Foo}/>
<Route path="bar" component={Bar}/>
</Route>
</Router>
</Provider>,
document.getElementById('mount')
);
33 changes: 33 additions & 0 deletions examples/basic/components/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const React = require('react');
const { Link } = require('react-router');
const { connect } = require('react-redux');
const { updatePath } = require('redux-simple-router');
const { increase, decrease } = require('../actions/count');

const App = React.createClass({
render: function() {
return (
<div>
<header>
Links:
{' '}
<Link to="/foo">Foo</Link>
{' '}
<Link to="/bar">Bar</Link>
</header>
{this.props.children}
<div>
{this.props.number}
<button onClick={() => this.props.increase(1)}>Increase</button>
<button onClick={() => this.props.decrease(1)}>Decrease</button>
<button onClick={() => this.props.updatePath('/foo')}>Decrease</button>
</div>
</div>
);
}
});

module.exports = connect(
state => ({ number: state.count.number }),
{ increase, decrease, updatePath }
)(App);
9 changes: 9 additions & 0 deletions examples/basic/components/Bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const React = require('react');

const Bar = React.createClass({
render: function() {
return <div>bar</div>;
}
});

module.exports = Bar;
9 changes: 9 additions & 0 deletions examples/basic/components/Foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const React = require('react');

const Foo = React.createClass({
render: function() {
return <div>foo</div>;
}
});

module.exports = Foo;
5 changes: 5 additions & 0 deletions examples/basic/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const App = require('./App');
const Foo = require('./Foo');
const Bar = require('./Bar');

module.exports = { App, Foo, Bar };
5 changes: 5 additions & 0 deletions examples/basic/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

module.exports = {
INCREASE: 'INCREASE',
DECREASE: 'DECREASE'
}
10 changes: 10 additions & 0 deletions examples/basic/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>redux-simple-router basic example</title>
</head>
<body>
<div id="mount"></div>
<script src="dist/bundle.js"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "rsr-basic-example",
"version": "0.0.0",
"dependencies": {
"history": "^1.13.1",
"react": "^0.14.2",
"react-dom": "^0.14.2",
"react-redux": "^4.0.0",
"react-router": "^1.0.0",
"redux": "^3.0.4",
"redux-simple-router": "0.0.8"
},
"devDependencies": {
"babel-core": "^6.1.21",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"webpack": "^1.12.6"
}
}
17 changes: 17 additions & 0 deletions examples/basic/reducers/count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const constants = require('../constants');

const initialState = {
number: 1
}

function update(state = initialState, action) {
if(action.type === constants.INCREASE) {
return { number: state.number + action.amount };
}
else if(action.type === constants.DECREASE) {
return { number: state.number - action.amount };
}
return state;
}

module.exports = update;
3 changes: 3 additions & 0 deletions examples/basic/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const count = require('./count');

module.exports = { count };
29 changes: 29 additions & 0 deletions examples/basic/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const path = require('path');

module.exports = {
entry: './app.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel?presets[]=react,presets[]=es2015'],
exclude: /node_modules/,
include: __dirname
}]
}
}



// This will make the redux-simpler-router module resolve to the
// latest src instead of using it from npm. Remove this if running
// outside of the source.
var src = path.join(__dirname, '..', '..', 'src')
var fs = require('fs')
if (fs.existsSync(src)) {
// Use the latest src
module.exports.resolve = { alias: { 'redux-simple-router': src } }
}