question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could mapStateToProps be an object?

See original GitHub issue

I’m playing around with selectors, and most of my mapStateToProps functions look like this:

const mapStateToProps = state => ({
  isAuthenticated: selectors.isAuthenticated(state),
  isSomethingElse: selectors.isSomethingElse(state),
});

Since I always pass on the state, would it be okay to do the same thing as mapDispatchToProps does, and just allow sending an object of functions? Like so:

const mapStateToProps = {
  isAuthenticated: selectors.isAuthenticated,
  isSomethingElse: selectors.isSomethingElse,
};

Then, connect could do the work required to call each of these with the state.

I’d be happy to submit a PR doing this - but what are your thoughts? I’m sure there’s a reason the API is the way it is 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
markeriksoncommented, Feb 3, 2019

Part of me still kinda wants do to this, but I’ll go along with Tim and say it’s not worth it to build it in.

2reactions
markeriksoncommented, Jan 22, 2019

I’m willing to potentially reconsider this as long as we keep it narrowly scoped in behavior and implementation.

No factory functions. No nesting.

I can see two useful forms:

  • An array of string key names
  • An object of selectors

Both are doable if we keep the behavior limited to basically what @jchook showed. Question is, whether there’s enough benefit for including that functionality.

Tweeted this question: https://twitter.com/acemarke/status/1087568080052596736

I’ll reopen this issue for the sake of discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect: Extracting Data with mapStateToProps
Your mapStateToProps function should return a plain object that contains the data the component needs: Each field in the object will become a ......
Read more >
mapStateToProps must return an object. Instead received ...
Your state appears to be a map from immutable.js, while mapStateToProps is supposed to return an object, as your error message says. Try...
Read more >
You Might Not Need The mapDispatchToProps Function
First, a mapStateToProps function that plucks pieces of state out of Redux and assigns them to props that your React component will use....
Read more >
What is mapStateToProps in Redux?
The mapStateToProps function is wrapped in a connect function provided by the React-Redux library. The connect function will allow four ...
Read more >
Manage state with React Hooks
You can use hooks to subscribe to the Redux store and dispatch actions, ... Implement mapStateToProps to have access to the state object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found