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.

RFC: `mapStateToProps` or `mapStoreToProps` naming convention?

See original GitHub issue

I’ve taught about 10 people how to use react-redux at this point and a common sticking point is the use of mapStateToProps. This seems to be due to confusion in terminology between local state and the state of the store.

In teaching these React-newbies, I’ve found that when I describe the mapStateToProps in terms of mapStoreToProps, they understand it very quickly, since it’s not immediately clear to them based on the name that the values they’re mapping in mapStateToProps come from the store.

I propose changing mapStateToProps to mapStoreToProps, as I think it more accurately reflects what the function is doing: it’s selecting values from the store and mapping them the component’s props.

const mapStoreToProps = store => {
  return {
    user: store.user
  }
}

I realize this is only a superficial change, since the name of the function doesn’t actually affect how connect uses it, but I think it might make it easier for React-newbies to understand what the function is actually doing.

Thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
gaearoncommented, Aug 19, 2016

This will however be wrong because store is a different thing. It’s an object with getState() and dispatch() methods. We can’t refer to store state as store because this is even more confusing. And we can’t pass store itself because we want to hide dipspatch by design.

5reactions
pandigitacommented, Apr 25, 2018

This is not just a minor issue, but I find that mapStateToProps is the first real stumbling block for people learning redux - and that’s not because of them but because it’s a really bad naming decision for something used in react-redux.

I’m teaching my students mapReduxStateToProps(), but that’s just one of various solutions. So independent of what the solution is, can we start with all acknowledging that mapStateToProps is letting beginners down?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
React Redux Tutorials - 26 - mapStateToProps - YouTube
Courses - https://learn.codevolution.dev/ Support UPI - https://support.codevolution.dev/ Support PayPal ...
Read more >
Rethinking React-Redux folder structure - RFCs - TheForeman
Do we really need the component name in each file name? I like the simplicity with the naming, as for actions.js, reducer.js, selectors.js...
Read more >
Understanding React-Redux and mapStateToProps()
The key is in the name mapStateToProps you are passing read-only properties for a component to consume. I'll often use my container components ......
Read more >
What is mapStateToProps in Redux? - DEV Community ‍ ‍
The mapStateToProps function is used in the Redux pattern to reflect any updates to the Redux store and merge them into props in...
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