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
import { HmrInitAction, HmrBeforeDestroyAction } from '@ngxs/hmr-plugin'; | |
@State({ ... }) | |
export class MyState { | |
@Action(HmrInitAction) | |
public hmrInit(ctx: StateContext, { payload }) { | |
ctx.setState({ ... }) | |
} | |
@Action(HmrBeforeDestroyAction) |
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
@State<AnimalsStateModel>({ | |
name: 'animals', | |
defaults: { | |
zebra: { | |
food: [], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' |
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 fiz = { foo: 'bar' }; | |
const baz = fiz; | |
baz.foo = 'qux'; // mutate field 'foo' | |
// because the references are equal | |
console.log(baz.foo); // qux | |
console.log(fiz.foo); // qux |
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
/* | |
You have to understand that each object | |
has a unique identity in JavaScript | |
console.log(new Object() === new Object()); | |
*/ | |
console.log({} === {}); // false |
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
@Component({ | |
selector: 'my-app', | |
template: ` | |
<b>default</b>: {{ todos$ | async | json }} <br> | |
<b>mutable reverse</b>: {{ mutableTodos$ | async | json }} <br> | |
<b>immutable reverse</b>: {{ immutableTodos$ | async | json }} <br> | |
` | |
}) | |
export class AppComponent { |
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
@State<number[]>({ | |
name: 'todos', | |
defaults: [1, 2, 3] | |
}) | |
export class AppState { | |
@Selector() public static mutableTodos(state: number[]): number[] { | |
return state.reverse(); | |
} |
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
<b>default</b>: {{ todos$ | async | json }} <br> | |
<b>immutable reverse</b>: {{ immutableTodos$ | async | json }} <br> | |
<b>mutable reverse</b>: {{ mutableTodos$ | async | json }} <br> |
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
@NgModule({ | |
imports: [ | |
BrowserModule, | |
NgxsModule.forRoot([ AppState ], { developmentMode: !env.production }) | |
], | |
... | |
}) | |
export class AppModule { } |
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 animals = { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' | |
} | |
}; |
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
/* | |
INCORRECT CHANGES (deep mutation) | |
*/ | |
const animals = { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { |