Created
August 8, 2017 12:31
-
-
Save suciuvlad/a7da4d6fef7bbc74eebc957d3350610b to your computer and use it in GitHub Desktop.
redux form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import { connect } from 'react-redux' | |
| import { resetForm } from './actions'; | |
| const getDisplayName = WrappedComponent => { | |
| return WrappedComponent.displayName || WrappedComponent.name; | |
| } | |
| const ReduxForm = (WrappedComponent, props) => { | |
| const displayName = getDisplayName(WrappedComponent); | |
| class Wrapper extends WrappedComponent { | |
| componentWillUnmount () { | |
| this.props.resetForm(); | |
| } | |
| getChildContext () { | |
| return { | |
| name: displayName, | |
| fields: props.fields | |
| } | |
| } | |
| } | |
| Wrapper.childContextTypes = { | |
| name: React.PropTypes.string, | |
| fields: React.PropTypes.array | |
| }; | |
| return connect( | |
| wrappedMapStateToProps(displayName), | |
| wrappedMapDispatchToProps(displayName) | |
| )(Wrapper); | |
| } | |
| const wrappedMapStateToProps = displayName => { | |
| return (state) => { | |
| const forms = state.get('formReducer').forms; | |
| return forms && forms[displayName] || {}; | |
| } | |
| } | |
| const wrappedMapDispatchToProps = displayName => { | |
| return (dispatch) => ({ | |
| resetForm: () => dispatch(resetForm(displayName)) | |
| }) | |
| } | |
| export default ReduxForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment