-
-
Save gaearon/64e2c4adce2b4918c96c3db2b44d8f68 to your computer and use it in GitHub Desktop.
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, { Component } from 'react'; | |
const counter = (state = { value: 0 }, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return { value: state.value + 1 }; | |
case 'DECREMENT': | |
return { value: state.value - 1 }; | |
default: | |
return state; | |
} | |
} | |
class Counter extends Component { | |
state = counter(undefined, {}); | |
dispatch(action) { | |
this.setState(prevState => counter(prevState, action)); | |
} | |
increment = () => { | |
this.dispatch({ type: 'INCREMENT' }); | |
}; | |
decrement = () => { | |
this.dispatch({ type: 'DECREMENT' }); | |
}; | |
render() { | |
return ( | |
<div> | |
{this.state.value} | |
<button onClick={this.increment}>+</button> | |
<button onClick={this.decrement}>-</button> | |
</div> | |
) | |
} | |
} |
@graphicore lol no, state is immutable
Probably a good interview question on how to write Redux style code
without using Redux
It's 2019. useReducer :)
It's 2019. useReducer :)
This is from 5 years ago.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From line 17:
should probably be more like: