Skip to content

Instantly share code, notes, and snippets.

@mhart
Last active January 2, 2016 13:49
Show Gist options
  • Save mhart/8312331 to your computer and use it in GitHub Desktop.
Save mhart/8312331 to your computer and use it in GitHub Desktop.
Simple example using CoffeeScript syntax with React (run with `coffee` command to verify output). Could get some nicer syntax by wrapping some of the `DOM` functions so that the first arg doesn't need to be `null` when there are no attributes - and also so that arrays could be splatted automatically for children if required.
React = require 'react'
{ul, li, div, h3, button, form, input} = React.DOM
TodoApp = React.createClass
getInitialState: ->
items: @props.items
text: ''
onKey: (e) ->
@setState text: e.target.value
handleSubmit: ->
@setState items: @state.items.concat(@state.text), text: ''
return false
render: ->
div null,
h3 null, 'Todo'
TodoList items: @state.items
form onSubmit: @handleSubmit,
input onKeyUp: @onKey, value: @state.text
button null, "Add ##{@state.items.length + 1}"
TodoList = React.createClass
render: ->
ul children: @props.items.map (text) ->
li null, text
# Alternatively, something like:
# ul null, @props.items.map((text) ->
# li null, text
# )...
React.renderComponentToString TodoApp(items: ['a', 'b', 'c']), console.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment