Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit 1c92cbf

Browse files
committed
simple-example: Add base redux implementation.
Adding the basis of a redux implementation to the simple example. Using the code from here as a base: https://github.com/rackt/redux/tree/master/examples/async This creates the base reducer/action and store code for redux. It is not yet used with any react components.
1 parent 1c9dd30 commit 1c92cbf

File tree

5 files changed

+116
-4
lines changed

5 files changed

+116
-4
lines changed

examples/simple/actions/primes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
export const ADD_PRIME = 'ADD_PRIME';
3+
export const ADD_NON_PRIME = 'ADD_PRIME';
4+
export const QUEUE_NUMBER = 'QUEUE_NUMBER';
5+
6+
export function testPrime( number ) {
7+
return {
8+
type: QUEUE_NUMBER,
9+
number
10+
};
11+
}
12+

examples/simple/containers/App.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { Component, PropTypes } from 'react';
2+
import { connect } from 'react-redux';
3+
4+
class App extends Component {
5+
constructor( props ) {
6+
super( props );
7+
}
8+
9+
render() {
10+
return (
11+
<div>
12+
<h1>redux-trigger simple example</h1>
13+
</div>
14+
);
15+
}
16+
}
17+
18+
App.propTypes = {
19+
dispatch: PropTypes.func.isRequired
20+
};
21+
22+
function mapStateToProps( state ) {
23+
return { };
24+
}
25+
26+
export default connect( mapStateToProps )( App );
27+

examples/simple/index.jsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import 'babel-core/polyfill';
22
import React from 'react';
33
import { render } from 'react-dom';
4+
import { Provider } from 'react-redux';
5+
import App from './containers/App';
6+
import configureStore from './store/configureStore';
47

5-
render(
6-
<h1>Test</h1>,
7-
document.getElementById( 'root' )
8-
);
8+
const store = configureStore();
9+
10+
const rootComponent =
11+
<Provider store={ store }>
12+
<App />
13+
</Provider>;
14+
15+
render( rootComponent, document.getElementById( 'root' ) );
916

examples/simple/reducers/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { combineReducers } from 'redux';
2+
import {
3+
ADD_PRIME,
4+
ADD_NON_PRIME,
5+
QUEUE_NUMBER
6+
} from '../actions/primes';
7+
8+
const primesInitialState = {
9+
primes: [],
10+
nonPrimes: [],
11+
queue: []
12+
};
13+
14+
function primesReducer( state = primesInitialState, action ) {
15+
switch ( action.type ) {
16+
case ADD_PRIME:
17+
const primes = [ ...state.primes, action.prime ];
18+
19+
return Object.assign( {}, state, {
20+
primes
21+
} );
22+
case ADD_NON_PRIME:
23+
const nonPrimes = [ ...state.nonPrimes, action.nonPrime ];
24+
25+
return Object.assign( {}, state, {
26+
nonPrimes
27+
} );
28+
case QUEUE_NUMBER:
29+
const queue = [ ...state.queue, action.number ];
30+
31+
return Object.assign( {}, state, {
32+
queue
33+
} );
34+
default:
35+
return state;
36+
}
37+
}
38+
39+
const rootReducer = combineReducers( {
40+
primesReducer
41+
} );
42+
43+
export default rootReducer;
44+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createStore, applyMiddleware } from 'redux';
2+
import createLogger from 'redux-logger';
3+
import rootReducer from '../reducers';
4+
5+
const createStoreWithMiddleware = applyMiddleware(
6+
createLogger()
7+
)( createStore );
8+
9+
export default function configureStore( initialState ) {
10+
const store = createStoreWithMiddleware( rootReducer, initialState );
11+
12+
if ( module.hot ) {
13+
// Enable Webpack hot module replacement for reducers
14+
module.hot.accept( '../reducers', () => {
15+
const nextRootReducer = require( '../reducers' );
16+
store.replaceReducer( nextRootReducer );
17+
} );
18+
}
19+
20+
return store;
21+
}
22+

0 commit comments

Comments
 (0)