ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.03k stars 1.97k forks source link

Signal Store with Angular SSR initiate store in every injected component #4284

Closed alexcibotari closed 5 months ago

alexcibotari commented 6 months ago

Which @ngrx/* package(s) are the source of the bug?

signals

Minimal reproduction of the bug/regression with instructions

  1. Create an Angular SSR Project
  2. Create a Signal Store with
    const TextStore = signalStore(
    { providedIn: 'root' },
    withState({text: 'init'}),
    withMethods( state => ({
     changeText: (text: string) => {
        patchState(state, { text});
      },
    }))
    }
  3. add console.log in onInit and onDestory Store Hook
  4. Create two different components: Header and Footer
  5. Inject the Signal Sotre in both components
  6. Render the components
  7. The Sotre is initialized twice, and I have two different instances of Store, check the logs

Logs

AppComponent: browser
app.component.ts:22 AppComponent: angular-ssr
core.mjs:29749 Angular is running in development mode.
cart.store.ts:107 onInit {items: Array(0)}
cart.component.ts:24 CartComponent effect []
cart.store.ts:107 onInit {items: Array(0)}
shopping-cart.component.ts:50 ShoppingCartComponent effect []

Expected behavior

I expect to have only one signal Store to keep the application State

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

"@ngrx/signals": "^17.1.1", "@ngrx/operators": "^17.1.1", Angular 17.3.x Windows 11 Chrome

Other information

No response

I would be willing to submit a PR to fix this issue

markostanimirovic commented 6 months ago

Hi @alexcibotari,

Can you check if you accidentally added CartStore to the providers array of CartComponent and/or ShoppingCartComponent?

The best would be if you could provide a GitHub repo / StackBlitz playground with a reproduction.

markostanimirovic commented 5 months ago

Feel free to reopen this issue with the provided reproduction if a bug exists.