concept connect function in category react native

appears as: connect function
React Native in Action

This is an excerpt from Manning's book React Native in Action.

8.6 Accessing data using the connect function

You access the Redux store from a child component by using the connect function from react-redux. The first argument to connect is a function that gives you access to the entire Redux state. You can then return an object with whatever pieces of the store you want access to.

Let’s see what this means by looking at the connect function you’ll use in the Books.js component.

Listing 8.5 connect function in Books.js
connect(
  (state) => {    #1  
    return {    #2  
      books: state.bookReducer.books
    }
  }
)(Books)    #3  

#1   Function that gives the global Redux state object
#2   Returns an object from this function
#3   Passes in Books

The first argument to connect is a function that gives the global Redux state object as an argument. You can then reference this state object and have access to anything in the Redux state. You return an object from this function. Whatever keys are returned in the object become available as props in the component you’re wrapping: in this case, Books. You pass in Books as the only argument to the connect function’s second function call.