Created
January 14, 2017 08:17
-
-
Save cellog/7aba126279dc7434c8083e90f603d0db to your computer and use it in GitHub Desktop.
rendering a component based on state, and whether that state is ready to be used or not
This file contains 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 from 'react' | |
import { connect } from 'react-redux' | |
export default (isActive, loaded = () => true, componentLoadingMap = {}) => | |
({ component = (props) => <div {...props} />, loading = () => null, ...props }) => { | |
const Component = component | |
const Loading = loading | |
if (componentLoadingMap.component) { | |
props.component = componentLoadingMap.component | |
props[componentLoadingMap.component] = undefined | |
} | |
if (componentLoadingMap.loading) { | |
props.loading = componentLoadingMap.loading | |
props[componentLoadingMap.loading] = undefined | |
} | |
const NullComponent = ({ '@@__isActive': active, '@@__loaded': loaded, ...props }) => ( | |
!loaded ? <Loading {...props} /> | |
: ( active ? <Component {...props} /> : null ) | |
) | |
const R = connect((state, props) => { | |
const __loaded = loaded(state, props) | |
return { | |
...props, | |
'@@__isActive': __loaded && isActive(state, props), | |
'@@__loaded': __loaded | |
} | |
})(NullComponent) | |
R.displayName = `Toggle(${Component.displayName || Component.name || 'Component'})` | |
return <R {...props} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment