Skip to content

Instantly share code, notes, and snippets.

@mitu217
Last active February 16, 2017 09:37
Show Gist options
  • Save mitu217/540d0b575f1d1dbc841456270c0b11fd to your computer and use it in GitHub Desktop.
Save mitu217/540d0b575f1d1dbc841456270c0b11fd to your computer and use it in GitHub Desktop.
React + Reduxサンプル(動作未検証)
import { createAction } from 'redux-actions';
export const increment = createAction('increment');
export default function counter(state = 0, action) {
switch(action.type) {
case 'increment':
return state + 1;
default:
return state;
}
}
import { combineReducers } from 'redux';
import counter from './counter';
const rootReducer = combineReducers({
counter
});
export default rootReducer;
import { createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import reducer from './reducers';
export default function configureStore(initialState) {
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
reducer,
initialState,
);
return store;
}
import React from 'react'
import { connect } from 'react-redux';
import { increment } from './actions';
class Main extends React.Component {
handleIncrement() {
this.props.dispatch(increment());
}
render() {
return(
<div>
<h1>Counter Example</h1>
<p>
Clicked: { this.props.counter } times
{" "}
<button className='increment' onClick={ this.handleIncrement.bind(this) }>+</button>
</p>
</div>
);
}
}
function mapStateToProps(state) {
return {
counter: state.counter
};
}
export default connect(mapStateToProps)(Main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment