Last active
February 16, 2017 09:37
-
-
Save mitu217/540d0b575f1d1dbc841456270c0b11fd to your computer and use it in GitHub Desktop.
React + Reduxサンプル(動作未検証)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createAction } from 'redux-actions'; | |
export const increment = createAction('increment'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function counter(state = 0, action) { | |
switch(action.type) { | |
case 'increment': | |
return state + 1; | |
default: | |
return state; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from 'redux'; | |
import counter from './counter'; | |
const rootReducer = combineReducers({ | |
counter | |
}); | |
export default rootReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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