Skip to content

Instantly share code, notes, and snippets.

@taylorc
Last active April 26, 2018 04:12
Show Gist options
  • Select an option

  • Save taylorc/18ee988fb3c5345c7346a5958e948e91 to your computer and use it in GitHub Desktop.

Select an option

Save taylorc/18ee988fb3c5345c7346a5958e948e91 to your computer and use it in GitHub Desktop.
import { Action, State, StateContext } from '@ngxs/store';
import { DecrementCount, IncrementCount } from './home.actions';
//step 1
export interface HomeStateModel {
count: number;
}
//step 2
@State<HomeStateModel>({
name: 'count',
defaults: {
count: 0
}
})
//step 3
export class HomeState {
@Action(IncrementCount)
incrementCount({ getState, setState }: StateContext<HomeStateModel>) {
const state = getState();
setState({
...state,
count: state.count + 1
});
}
@Action(DecrementCount)
decrementCount({ getState, setState }: StateContext<HomeStateModel>) {
const state = getState();
setState({
...state,
count: state.count - 1
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment