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

Pass state to listeners as an argument? #1513

Closed
Lucretiel opened this issue Mar 11, 2016 · 3 comments
Closed

Pass state to listeners as an argument? #1513

Lucretiel opened this issue Mar 11, 2016 · 3 comments

Comments

@Lucretiel
Copy link

@Lucretiel Lucretiel commented Mar 11, 2016

This seems so straightforward and obvious I feel like I'm missing some reason it isn't done this way. Why don't listeners accept the new state as an argument?

let unsubscribe = store.subscribe(state => {
    ...
});
@merk
Copy link
Contributor

@merk merk commented Mar 11, 2016

Just use getState on the store.

let unsubscribe = store.subscribe(() => {
   const state = store.getState()

// ...
})

@Lucretiel
Copy link
Author

@Lucretiel Lucretiel commented Mar 11, 2016

I know, but why require the extra step? Especially considering how prevalently injection/control inversion is used elsewhere in redux?

@gaearon
Copy link
Contributor

@gaearon gaearon commented Mar 11, 2016

This has been discussed before: #303. subscribe() and getState() are orthogonal on purpose so if you “override” one in a store enhancer, you don’t need to override the other.

In any case, having just the new state is not very useful. You’ll probably want the previous state too. And not just the previous state—probably a specific part you care about. At which point you might as well write your own helper to do this.

See #303 (comment) for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants