-
-
Save angelogulina/d190f99fe8822a62cb6864b67a94f6f5 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment