Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect, Provider } from 'react-redux';
  3. import { createStore, compose } from 'redux';
  4. import { render } from 'react-dom';
  5.  
  6. function capitalize(str) {
  7. return str.split('').map((s, i) => {
  8. if (i === 0) return s.toUpperCase();
  9. return s;
  10. }).join('');
  11. }
  12.  
  13. function withState(state) {
  14. return function ComponentWithState(WrappedComponent) {
  15. return class extends Component {
  16. state = state;
  17. handleStateChange(key) {
  18. return (value = state[key], callback = null) => {
  19. return this.setState({ [key]: value }, callback);
  20. }
  21. }
  22. get stateHandlers() {
  23. return Object
  24. .keys(this.state)
  25. .reduce(
  26. (handlers, key) => {
  27. return {
  28. ...handlers,
  29. [`update${capitalize(key)}`]: this.handleStateChange(key),
  30. };
  31. },
  32. {}
  33. )
  34. }
  35. render() {
  36. return (
  37. <WrappedComponent
  38. {...this.stateHandlers}
  39. {...this.state}
  40. {...this.props}
  41. />
  42. );
  43. }
  44. }
  45. }
  46. }
  47.  
  48. function App(props) {
  49. const newLastName = props.lastName === 'Xalambrí'
  50. ? 'Caeiro'
  51. : undefined;
  52.  
  53. return (
  54. <h1
  55. onClick={props.handleClick(
  56. props.name.split('').reverse().join('')
  57. )}
  58. >
  59. hola {props.name} {props.lastName}
  60. <button onClick={() => props.updateLastName(newLastName)}>
  61. change last name
  62. </button>
  63. </h1>
  64. );
  65. }
  66. App.mapState = (state, props) => {
  67. return { name: state.name };
  68. }
  69. App.mapDispatch = (dispatch, props) => {
  70. return {
  71. handleClick: (name) => () =>
  72. dispatch({ type: 'UPDATE', name }),
  73. };
  74. }
  75.  
  76. // class App extends Component {
  77. // static mapState(state, props) {
  78. // return { name: state.name };
  79. // }
  80. // static mapDispatch(dispatch, props) {
  81. // return {
  82. // handleClick: (name) => () =>
  83. // dispatch({ type: 'UPDATE', name }),
  84. // };
  85. // }
  86. // render() {
  87. // return (
  88. // <h1
  89. // onClick={this.props.handleClick(
  90. // this.props.name.split('').reverse().join('')
  91. // )}
  92. // >
  93. // <Test /> {this.props.name}
  94. // </h1>
  95. // );
  96. // }
  97. // }
  98.  
  99. function container(WrappedComponent) {
  100. const mapState = WrappedComponent.mapState || null;
  101. const mapDispatch = WrappedComponent.mapDispatch || null;
  102. return connect(mapState, mapDispatch)(WrappedComponent);
  103. }
  104.  
  105. const AppContainer = withState({
  106. lastName: 'Xalambrí',
  107. })(container(App));
  108.  
  109. function reducer(state, action) {
  110. if (action.type === 'UPDATE') {
  111. return { name: action.name }
  112. }
  113. return state;
  114. }
  115. const store = createStore(reducer, { name: 'Sergio' });
  116. render(
  117. <Provider store={store}>
  118. <AppContainer />
  119. </Provider>,
  120. document.body
  121. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement