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.
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.
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.
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.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With