Last active
January 2, 2016 13:49
-
-
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.
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
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