Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using compose() and connect() together in React JS redux

Tags:

I am starting to develop a web application using React JS. I bought a theme from theme forest. In the theme, they are using compose like this in the component.

...Other code here  Login.propTypes = {       classes: PropTypes.shape({}).isRequired,       width: PropTypes.string.isRequired     };      export default compose(withWidth(), withStyles(themeStyles, { withTheme: true }))(Login); 

As you can see their code is using the compose at the end when exporting the Component. I cannot modify their built-structure. What I like to do now is I like to use connect feature of the react as well.

Normally connect is used in the place of compose. Now, if I want to use connect to work with the state of the application, how can I use it together with compose ?

like image 704
Wai Yan Hein Avatar asked Jul 10 '18 12:07

Wai Yan Hein


People also ask

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

Overview​ 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.

What is the use of compose in React Redux?

Compose is used when you want to pass multiple store enhancers to the store. Store enhancers are higher order functions that add some extra functionality to the store. The only store enhancer which is supplied with Redux by default is applyMiddleware however many other are available.

Can I pass multiple components within connect Redux?

Since you can have only one default export, you'd have to use named export in this case or split up those two components in two files.

When to use Connect in React 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() .


2 Answers

const enhance = compose(     withRouter,     withStyles(styles, 'some style'),     connect(mapStateToProps, mapDispatchToProps),     ....  export default enhance(MyComponent); 
like image 70
Safi Nettah Avatar answered Sep 17 '22 15:09

Safi Nettah


import {bindActionCreators} from 'redux'; import compose from 'recompose/compose'; import { connect } from 'react-redux'; ...Other code here function mapStateToProps(state) {     return {         //return state     } } function mapDispatchToProps(){     return bindActionCreators({         //actions     }, dispatch); } Login.propTypes = {     classes: PropTypes.shape({}).isRequired,     width: PropTypes.string.isRequired }; export default compose(withWidth(), withStyles(styles, {withTheme: true}), connect(mapStateToProps, mapDispatchToProps))(Login); 

I hope this solves your problem.

like image 32
Gaurav Bharti Avatar answered Sep 17 '22 15:09

Gaurav Bharti