Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is preferred way to use connect() from react-redux, Parent vs Child component?

I am little confuse in the efficient way to use connect() from react-redux library. I have the blow component

class SignUp extends React.Component {
//some functions
render(){
  return (
      <SignUpPageWrapper>
        <SignUpPage>
          <SignUpPageLeft>
            <SignUpLeftText /> //component
          </SignUpPageLeft>
          <SignUpPageRight>
            <SignUpForm /> //component <=
          </SignUpPageRight>
        </SignUpPage>
        <SignUpFooter /> //component
      </SignUpPageWrapper>
  );
 }
}

In above code, few are react Component(//commented) and rest are const from styled-component library.

As of now, I have made SignUpForm as a container, i.e wrapped into connect()

class SignUpForm extends React.Component {
  //lots of code here using this.props from connect()

}    
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SignUpForm);

But I feel that this is not the efficient way to use connect, the better would be to wrap the parent component SignUp into connect like below, and then passing the methods and states as props to the child components.

class SignUp extends React.Component {
    //some functions
    render(){
      return (
          //other components       
          <SignUpPageRight>
            <SignUpForm signupFunc={this.props.signupFunc} />
          </SignUpPageRight>
        </SignUpPage>
        <SignUpFooter /> //component
      </SignUpPageWrapper>
  );
 }
}
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SignUp);

What would be the clean, efficient and good way to write this code?

like image 796
Puspender Avatar asked Jan 31 '19 18:01

Puspender


People also ask

What is the purpose of the connect () function in Redux?

The React Redux connect() API is used for creating container elements that are connected to the Redux store. The Redux store is derived from the topmost ancestor of the component using React Context. If you are only creating a presentational component, you have no need for connect() .

How can we use Connect From React Redux?

The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.

Which component takes care of connecting to the Redux store for data fetching and state updates?

The <Provider /> component is part of the react-redux library and simply puts the store into the context which makes it available to its child components. This component does nothing else. connect() is how we can get state and dispatch actions from the store to our components.


1 Answers

If we go by documentation, there is actually a right or wrong answer to this. Redux documentation suggests separating components into presentational and container components.

React bindings for Redux separate presentational components from container components. This approach can make your app easier to understand and allow you to more easily reuse components. Redux Documentation

Your SignUpForm component is a perfect example of a container component that has the job of communicating with the redux store. Values, and dispatch handlers should be passed down through props to your presentational components. This makes your store manipulation very easy to understand because it is all contained within one component.

Now for reusability. Your container component should be higher-ordered enough so that it can contain all the presentational components that will manipulate a logical slice of your redux store, but low enough that you can reuse as much as possible. So for a form, the container should contain all the inputs, and you can separate the inputs themselves into as many presentational components as you like. This also allows you to reuse your presentational components with a different continainer component as long as you structure them with modularity in mind.

like image 178
Chase Avatar answered Sep 19 '22 05:09

Chase