Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the necessity of "export default connect" when you are connecting your react component to redux store

I was trying to create some components in the same jsx file. I wants to connect one of the child component to store(feels wrong here), without connecting the HOC component to the store. Therefore when connecting the child component he does not use export default connect. It creates an error saying actions are undefined.

Example code.

class Test1 extends React.Component {
   constructor(props) {
   ****
   } 
   render(){
   ****
   }
}
function mapActionsToProps(dispatch) {
   return {
       actions: {
          testAction: bindActionCreators(testActionFromActions, dispatch)
       }
   }
connect(mapStateToProps,mapActionsToProps) (Test1);

class Test2 extends React.Component {
   constructor(props){
   ***
   }
   render(){
      return (<Test1/>);
   }
}

export default Test2;

My question is why we always need to use connect as export default connect ?. Is there any other ways to do the connect?.

Thank you in advance.

like image 636
Shashith Darshana Avatar asked Sep 18 '17 10:09

Shashith Darshana


People also ask

Why we use export default in React?

Export default means you want to export only one value the is present by default in your script so that others script can import that for use. This is very much necessary for code Reusability.

What is the use of Connect in 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.

How does export default work in React?

Default Exports in ReactDefault export is used to export a single class, primitive, or function from a module. There are different ways to use default export . Usually, the default export is written after the function, like the example below.

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.


2 Answers

connect() returns a new connected component. In fact, connect() wraps your component in a new one, adding some useful features, that is why you usually export the return value of the connect function (the new component).

In your case, you should do something like

class Test1 extends React.Component {
   constructor(props) {
   ****
   } 
   render(){
   ****
   }
}
function mapActionsToProps(dispatch) {
   return {
       actions: {
          testAction: bindActionCreators(testActionFromActions, dispatch)
       }
   }
const ConnectedTest1 = connect(mapStateToProps,mapActionsToProps) (Test1);

class Test2 extends React.Component {
   constructor(props){
   ***
   }
   render(){
      return (<ConnectedTest1/>);
   }
}

export default Test2;
like image 106
Brendan Rius Avatar answered Oct 06 '22 01:10

Brendan Rius


With a little bit of more researching I found a cure :D.

connect always returns a function. In my given example I input Test1 as an argument to the connect function.

So we need to assign the returned function to some variable or const. In the above example the line connect(mapStateToProps,mapActionsToProps) (Test1); should change to const ConnectedComponent = connect(mapStateToProps,mapActionsToProps) (Test1);. After that I can export the component(ConnectedComponent) or reuse it within the same file.

this link help me to understand this.

Hope this answer will help others.

like image 44
Shashith Darshana Avatar answered Oct 05 '22 23:10

Shashith Darshana