ngxs / store

🚀 NGXS - State Management for Angular
http://ngxs.io
MIT License
3.54k stars 402 forks source link

The state created with undefined initial state even when defaults are specified #902

Closed catchshyam closed 5 years ago

catchshyam commented 5 years ago

I'm submitting a...


[x ] Regression (a behavior that used to work and stopped working in a new release)
[ x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => https://github.com/ngxs/store/blob/master/CONTRIBUTING.md
[ ] Other... Please describe:

Current behavior

The feature state object is created with initial state as undefined (even when the defaults is provided).

Expected behavior

The feature state object must use the values given in defaults object definition as initial state.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/ngxs-no-initial-state

Launch the application and click on the hyperlink in home page. Then on the console, you will see the debug message with initial state object.

Environment


Libs:
- @angular/core version: 7.2.5
- @ngxs/store version: 3.4.1
- @ngxs-labs/emitter: 1.9.0
- @ngxs-labs/immer-adapter: 1.1.2            
splincode commented 5 years ago

@markwhitfeld very strange

image

image

splincode commented 5 years ago

@arturovt problem with emitter plugin?

kuhnroyal commented 5 years ago

I can confirm this happening with 3.4.0/3.4.1

markwhitfeld commented 5 years ago

This only affects Emitter plugin users. This is because of the way that Emitter inherits from Store to create an EmitStore. I have found the point at which the state is reset. See this stackblitz: https://stackblitz.com/edit/ngxs-no-initial-state-debug

This bug is caused by this change in PR #791 https://github.com/ngxs/store/pull/791/files#diff-084c7b85b6562e3126b58451d5f8943aR20

Ngxs does not expect anyone to inherit from Store and essentially run this code twice. Emitter does some things with Ngxs internals that are not usual plugin behaviours.

splincode commented 5 years ago

fixed in 3.4.2

xmlking commented 5 years ago

when we have default state and partial state coming from NgxsStoragePluginModule default state is still coming as undefined

Store https://github.com/xmlking/ngx-starter-kit/blob/develop/libs/auth/src/lib/auth.state.ts#L31 NgxsStoragePluginModule https://github.com/xmlking/ngx-starter-kit/blob/develop/libs/core/src/lib/core.module.ts#L66

image

if I remove my data from LocalStorage and reload page then i see Auth Mode Changed: undefined => ImplicitFLow

image

splincode commented 5 years ago

@markwhitfeld

synec commented 5 years ago

I facing the same issue as @xmlking When state defaults are defined and the keys get stored via NgxsStoragePluginModule the defaults are not loaded.

splincode commented 5 years ago

@xmlking Please try new version!

npm install @ngxs/store@dev
splincode commented 5 years ago

Released in 3.5.0

cihataykas commented 1 year ago

@splincode the issue is still reproducible in 3.7.3