Skip to content

Instantly share code, notes, and snippets.

@itzzmeakhi
Created January 11, 2022 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzzmeakhi/37882001d37915a5d7ee1a966d6fdf46 to your computer and use it in GitHub Desktop.
Save itzzmeakhi/37882001d37915a5d7ee1a966d6fdf46 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { store } from './redux/store';
const connectFn = (mapStateToProps, mapDispatchToProps) => {
return (WrappedComponent) => {
return class extends Component {
unsubscribeTheStore = null;
componentDidMount() {
this.unsubscribeTheStore = store.subscribe(this.handleStateChange);
}
componentWillUnmount() {
this.unsubscribeTheStore();
}
handleStateChange = () => {
this.forceUpdate();
}
render() {
return (
<WrappedComponent
{...this.props}
{...mapStateToProps(store.getState(), this.props)}
{...mapDispatchToProps(store.dispatch, this.props)}
/>
);
}
}
}
}
export { connectFn };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment