Last active
February 23, 2017 17:50
-
-
Save robinweser/8f77040e2708975b3956db44068b853a to your computer and use it in GitHub Desktop.
Styles as functions of state
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 { createRenderer, render } from 'fela' | |
// creates a new renderer to render styles | |
const renderer = createRenderer() | |
// rules are just plain functions | |
// returning a valid style object | |
const rule = state => ({ | |
color: state.follower >= 20 ? 'green' : (state.follower >= 10 ? 'yellow' : 'red') | |
}) | |
const View = state => { | |
// rendering rules returns a className reference | |
// which can be attached to any element | |
const className = renderer.renderRule(rule, state)) | |
return ( | |
<div className={className} /> | |
You got {state.follower} followers! | |
<div> | |
) | |
} | |
// renders all styles into the DOM | |
render(renderer, document.getElementById('stylesheet')) | |
ReactDOM.render(<View follower={15} />, document.body) |
angelogulina
commented
Feb 23, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment