Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add support to the NgRx Store for dispatching actions that read signals #4537

Open
2 tasks
brandonroberts opened this issue Sep 26, 2024 · 0 comments · May be fixed by #4600
Open
2 tasks

Feature: Add support to the NgRx Store for dispatching actions that read signals #4537

brandonroberts opened this issue Sep 26, 2024 · 0 comments · May be fixed by #4600

Comments

@brandonroberts
Copy link
Member

Which @ngrx/* package(s) are relevant/related to the feature request?

store

Information

Had a discussion started by @manfredsteyer on using an effect to dispatch actions. Straightforward example.

class MyComponent {
  id = input();

  constructor() {
    effect(() => {
     store.dispatch(Actions.myAction(this.id()));
    });
  }
}

This works fine, but is a bit repetitive and requires the use of an effect in user-land code.

This would potentially make this a first class method on the Store, such as:

store.dispatch(() => Actions.myAction(this.id()));

This would listen for changes to the signal(s) and run the dispatch callback. It would also be run in the correct injection context using logic similar to the PR below, so it's cleaned up correctly when used at the component level even though the Store is a root provider.

#4529

This would also encourage less use of effect in user-land code for this use case.

Describe any alternatives/workarounds you're currently using

Use a Signal Store in the component as a proxy between the component and global Store.

const MyStore = signalStore(
  withMethods((globalStore = inject(Store)) => ({
    listenToID: rxMethod<string>(trigger$ =>
      trigger$.pipe(tap(val => {
        globalStore.dispatch(Actions.myAction(val))
      }))
  }))
)
class MyComponent {
  id = input<string>();
  store = inject(MyStore);

  constructor() {
     store.listenToID(this.id);
  }
}

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

  • Yes
  • No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants