Last active
October 10, 2017 16:48
-
-
Save emadb/3031569102a70ad2119fbadd04dd9f72 to your computer and use it in GitHub Desktop.
medium - react and redux
This file contains hidden or 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
| const dispatcher = { | |
| subscribers: [], | |
| register(subscriber) { | |
| this.subscribers.push(subscriber) | |
| }, | |
| dispatch(action) { | |
| this.subscribers.forEach(s => s(action)) | |
| } | |
| } |
This file contains hidden or 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
| class MyCounter extends React.Component { | |
| constructor() { | |
| super() | |
| this.state = { counter: 0 } | |
| } | |
| plusOne() { | |
| this.setState({counter: this.state.counter + 1}) | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| <div>Click count: {this.state.counter}</div> | |
| <button onClick={this.plusOne.bind(this)}>Add 1</button> | |
| </div> | |
| ) | |
| } | |
| } |
This file contains hidden or 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
| class WithState extends React.Component { | |
| constructor() { | |
| super() | |
| this.state = { counter: 0 } | |
| } | |
| plusOne() { | |
| this.setState({counter: this.state.counter + 1}) | |
| } | |
| render() { | |
| return ( | |
| <MyCounter value={this.state.counter} onPlusOne={this.plusOne.bind(this)} /> | |
| ) | |
| } | |
| } | |
| class MyCounter extends React.Component { | |
| render() { | |
| return ( | |
| <div> | |
| <div>Click count: {this.props.value}</div> | |
| <button onClick={this.props.onPlusOne}>Add 1</button> | |
| </div>) | |
| } | |
| } |
This file contains hidden or 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
| function MyCounter({value, onPlusOne}) { | |
| return ( | |
| <div> | |
| <div>Click count: {value}</div> | |
| <button onClick={onPlusOne}>Add 1</button> | |
| </div>) | |
| } |
This file contains hidden or 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
| function counterReducer(state, action) { | |
| return {counter: state.counter + action.value} | |
| } | |
| function createWithState(WrappedComponent, reducer, INITIAL_STATE) { | |
| return class WithState extends React.Component { | |
| constructor() { | |
| super() | |
| this.state = INITIAL_STATE | |
| } | |
| componentWillMount() { | |
| dispatcher.register(action => { | |
| const nextState = reducer(this.state, action) | |
| this.setState(nextState) | |
| }) | |
| } | |
| render() { | |
| return ( | |
| <WrappedComponent {...this.state} /> | |
| ) | |
| } | |
| } | |
| } | |
| function MyCounter({counter}) { | |
| return ( | |
| <div> | |
| <div>Click count: {counter}</div> | |
| <button onClick={() => dispatcher.dispatch({value: 1})}>Add 1</button> | |
| </div>) | |
| } | |
| const MyComponent = createWithState(MyCounter, counterReducer, {counter: 0}) |
This file contains hidden or 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
| function counterReducer(state, action) { | |
| return {counter: state.counter + action.value} | |
| } | |
| class WithState extends React.Component { | |
| constructor() { | |
| super() | |
| this.state = { counter: 0 } | |
| } | |
| componentWillMount() { | |
| dispatcher.register(action => { | |
| const nextState = counterReducer(this.state, action) | |
| this.setState(nextState) | |
| }) | |
| } | |
| render() { | |
| return ( | |
| <MyCounter value={this.state.counter} /> | |
| ) | |
| } | |
| } | |
| function MyCounter({value, onPlusOne}) { | |
| return ( | |
| <div> | |
| <div>Click count: {value}</div> | |
| <button onClick={() => dispatcher.dispatch({value: 1})}>Add 1</button> | |
| </div>) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment