Created
October 25, 2017 20:13
-
-
Save estrattonbailey/e91593a2ea750b4fd61312257a681fa6 to your computer and use it in GitHub Desktop.
Refunk v2 Example
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 preact from 'preact' | |
import connect from 'refunk' | |
// pass only select props to child | |
function pear (mapProps) { | |
return Component => props => ( | |
<Component {...mapProps(props)} /> | |
) | |
} | |
// connect helper | |
function state (map) { | |
return function WrappedComponent (comp) { | |
return connect(pear(map)(comp)) | |
} | |
} | |
// provider | |
const App = connect(class extends preact.Component { | |
componentWillMount () { | |
fetchHome().then(data => this.props.update(state => ({ | |
globals: data | |
}))) | |
} | |
render (props, state) { | |
return props.globals ? ( | |
<main> | |
{props.children} | |
</main> | |
) : 'loading' | |
} | |
}) | |
// your component | |
const Component = state(props => ({ | |
images: props.page.galleryImages | |
}))(props => ( | |
<p>State is loaded!</p> | |
)) | |
// app root | |
preact.render(( | |
<App> | |
<Component /> | |
</App> | |
), document.getElementById('root')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment