Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. const foo1 = () => { this.rawr = "rawr" }
  2. function foo2 () { this.rawr = "rawr" }
  3.  
  4. function moo2 () { this.rawr = "moo" }
  5. function resetRawr () { this.rawr = "rawr" }
  6. function setRawr (value) { this.rawr = value }
  7.  
  8. const rawrStuff = { moo2: moo2, resetRawr: resetRawr, setRawr: setRawr }
  9.  
  10. foo1()
  11.  
  12.  
  13. function Foo () {
  14. this.rawr = "rawr"
  15. this.resetRawr = function () { this.rawr = "rawr"}
  16. }
  17.  
  18. Foo.prototype.changeRawr = function (value) { this.rawr = value }
  19.  
  20.  
  21. const foo = new Foo()
  22. foo.changeRawr("omg")
  23.  
  24. Object.assign(foo.prototype, rawrStuff, { foo2 })
  25.  
  26. foo.rawr // "omg"
  27.  
  28.  
  29. foo.moo2()
  30.  
  31.  
  32.  
  33.  
  34. class Foo extends React.Componet{
  35. constructor (props) {
  36. super(props)
  37. this.rawr = "rawr"
  38. }
  39.  
  40. resetRawr = function () { this.rawr = "rawr" }
  41.  
  42. changeRawr(value) { this.rawr = value }
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. import { createStore } from 'fe-lib-store'
  50. import { connect } from 'fe-hoc-connect'
  51. import { compose } from 'fe-hoc-compose'
  52.  
  53. const initialState = {
  54. counterOne: 0,
  55. counterTwo: 0,
  56. counterThree: 0,
  57. }
  58.  
  59. const { store } = createStore('counters', {}, initialState)
  60.  
  61. const connect = (store, mapState) => Comp => {
  62. return class extends React.Component {
  63. componentDidMount () {
  64. this.mount = true
  65. this.unsub = store.subscribe(() => this.mount && this.forceUpdate())
  66. }
  67.  
  68. componentWillUnmount () {
  69. this.mount = false
  70. this.unsub()
  71. }
  72.  
  73. render () {
  74. return <Comp {...this.props} {...mapState(store.getState(), this.props)}/>
  75. }
  76. }
  77. }
  78.  
  79. const Btn = ({ children, onClick }) =>
  80. <button onClick={onClick}>
  81. { children }
  82. </button>
  83.  
  84. const Text = ({ children }) =>
  85. <strong>{ children }</strong>
  86.  
  87. const Counter = ({ count, onInc, onDec }) =>
  88. <div>
  89. <Text>{count}</Text>
  90. { onDec && <Btn onClick={onDec}>-</Btn> }
  91. { onInc && <Btn onClick={onInc}>+</Btn> }
  92. </div>
  93.  
  94. const ConnectedCounter = connect(store, (state, { id }) => ({
  95. count: state[id],
  96. }))(Counter)
  97.  
  98. const Counters = ({ counters, total }) =>
  99. <div>
  100. <Title total={total}>Count</Title>
  101. { counters.map(id => <ConnectedCounter id={id}/>) }
  102. </div>
  103.  
  104. const ConnectedCounters = compose(
  105. connect(otherStore, (state, props) => ({
  106. activeCounter: state.activeCounter,
  107. })),
  108. connect(store, (state, props) => ({
  109. counters: Object.keys(state),
  110. total: Object.keys(state).map(id => state[id]).reduce((a, b) => a+b),
  111. }))
  112. )(Counters)
  113.  
  114. <ConnectedCounters/>
Add Comment
Please, Sign In to add comment