Skip to content

Instantly share code, notes, and snippets.

@deptno
Created July 17, 2018 15:30
Show Gist options
  • Save deptno/2c55100a5d5f96b17219e9c599a28052 to your computer and use it in GitHub Desktop.
Save deptno/2c55100a5d5f96b17219e9c599a28052 to your computer and use it in GitHub Desktop.
immutable class
class A {
state = {
x: 0,
y: 1
}
constructor(initialState?: A) {
if (initialState) {
this.state = initialState
}
}
setX(x): A {
return new A({...this.state, x})
}
setY(): void {
// legacy
}
}
const reducer = (state: A, action) => {
switch (action.type) {
case SET_X:
return state.setX(action.payload)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment