Guest User

Untitled

a guest
Jul 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. const ACTION = 'ACTION';
  2. const defaultState = {
  3. value: 5
  4. };
  5. const doAction = ()=>{
  6. return {
  7. type: ACTION
  8. };
  9. };
  10.  
  11. const reducer = (state = defaultState, action) => {
  12. let update = Object.assign({}, state);
  13. switch(action.type){
  14. case ACTION:
  15. update.value = state.value + 1;
  16. return update;
  17. default:
  18. return state;
  19. }
  20. };
  21.  
  22. const store = Redux.createStore(reducer);
  23.  
  24. class App extends React.Component {
  25. constructor(props){
  26. super(props);
  27. this.handleClick = this.handleClick.bind(this);
  28. }
  29.  
  30. handleClick(e){
  31. this.props.doAction();
  32. }
  33.  
  34. render(){
  35. return(
  36. <div id='app'>
  37. <button onClick={this.handleClick}>Click</button>
  38. <p>{this.props.value}</p>
  39. </div>
  40. );
  41. }
  42. }
  43.  
  44. const Provider = ReactRedux.Provider;
  45.  
  46. const mapStateToProps = (state)=> {
  47. return {
  48. value: state.value
  49. };
  50. }
  51.  
  52. const mapDispatchToProps = (dispatch) => {
  53. return {
  54. doAction: () => {
  55. dispatch(doAction())
  56. }
  57. };
  58. }
  59.  
  60. const Container = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(App);
  61.  
  62. class AppWrapper extends React.Component {
  63. constructor(props){
  64. super(props);
  65. }
  66. render(){
  67. return(
  68. <Provider store={store}>
  69. <Container />
  70. </Provider>
  71. );
  72. }
  73.  
  74. }
  75.  
  76. ReactDOM.render(<AppWrapper />, document.getElementById('root'));
Add Comment
Please, Sign In to add comment