Skip to content

Instantly share code, notes, and snippets.

@quangv
Last active October 4, 2017 06:04
Show Gist options
  • Save quangv/52d7cf1b39b0611b4029e309e47944e2 to your computer and use it in GitHub Desktop.
Save quangv/52d7cf1b39b0611b4029e309e47944e2 to your computer and use it in GitHub Desktop.
Redux HOC Wrapper
Copyright 2017 Quang Van
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// @flow
import * as React from 'react'
import { isCollection } from 'immutable'
// you need at least Immutable.js 4.x
/* Pass regular JS objects instead of Immutables to "dumb" components.
*
* http://redux.js.org/docs/recipes/UsingImmutableJS.html#use-a-higher-order-component-to-convert-your-smart-components-immutablejs-props-to-your-dumb-components-javascript-props
*
* https://gist.github.com/quangv/52d7cf1b39b0611b4029e309e47944e2
*/
export default (Component: React.ComponentType<any>) => (props: Object) => {
const propsJS = Object.keys(props).reduce( function(newProps, key) {
const value = props[key]
if (isCollection(value)) {
newProps[key] = value.toJS() // convert Immutable to regular JS.
} else {
newProps[key] = value
}
return newProps
}, {} )
return <Component {...propsJS} />
}
import * as React from 'react'
import { isCollection } from 'immutable'
// you need at least Immutable.js 4.x
/* Pass regular JS objects instead of Immutables to "dumb" components.
*
* http://redux.js.org/docs/recipes/UsingImmutableJS.html#use-a-higher-order-component-to-convert-your-smart-components-immutablejs-props-to-your-dumb-components-javascript-props
*/
export default Component => props => {
const propsJS = Object.keys(props).reduce( function(newProps, key) {
const value = props[key]
if (isCollection(value)) {
newProps[key] = value.toJS() // convert Immutable to regular JS.
} else {
newProps[key] = value
}
return newProps
}, {} )
return <Component {...propsJS} />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment