DEV Community

Domitrius
Domitrius

Posted on

Explain Redux's connect() and mapStateToProps like I'm 5

Would love to hear some different explanations around this piece of Redux! Thanks in advance :)

Top comments (4)

Collapse
 
remojansen profile image
Remo H. Jansen

The Store is a box, which contains the entire application state. Imagine that this box is in an unknown location.

The components need to take some of the things out of the box but they only need some of the things stored in it. The components know what they need from the box but they don't know where the box is.

The mapStateToProps function is a filter used to select which things in the box are required by the component. The selected things become the component properties.

The mapStateToProps function is not enough because it selects the required things in the box but it doesn't know where the box is located.

The connect function knows where the box is located and passes it to the mapStateToProps function so it can grab what it needs.

Collapse
 
hitesh9545 profile image
hitesh9545

Very well explained .. Thanks..

Collapse
 
domitriusclark profile image
Domitrius

Great explanation. The image is a perfect pairing! Thank you.

Collapse
 
gvssaisumanth profile image
gvssaisumanth

Thank you that was great...