33
loading...
This website collects cookies to deliver better user experience
connect()
function in React Native. connect()
function is like the middle man between a component and the Redux store. It provides the component with the data it needs from the store, and the tools which the component can use to influence the data in the store.connect()
function has four(4) parameters, all of which are optional.mapStateToProps
, mapDispatchToProps
, mergeProps
and options
. I will elaborate further on each in later articles, but for now I want to use the analogy of a school canteen to make it easier to grasp the role of each parameter.mapStateToProps
form allows each student to specify what exactly they want from the store. This way the clerks know what to provide each student.mapDispatchToProps
form gives every student the chance to give feedback to the canteen. Each student will be given a next form if they checked a box on the mapDispatchToProps
form.options
parameter.stateProps
, which are returned by mapStateToProps
.dispatchProps
, which are returned by mapDispatchToProps
. ownProps
, which are the props passed into the component. (Not familiar with this, then check out my article).mergeProps
, which assembles the items above into the props
box for the student.