Skip to content

Instantly share code, notes, and snippets.

@robinweser
Last active February 23, 2017 17:50
Show Gist options
  • Save robinweser/8f77040e2708975b3956db44068b853a to your computer and use it in GitHub Desktop.
Save robinweser/8f77040e2708975b3956db44068b853a to your computer and use it in GitHub Desktop.
Styles as functions of state
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
Copy link

const className = renderer.renderRule(rule, state)) // <- too much parenthesis?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment