From 42ff8cf7dc23eb3214f19b52c475df58747912a7 Mon Sep 17 00:00:00 2001 From: jordow Date: Wed, 5 Jun 2013 23:45:34 -0700 Subject: [PATCH] Make todo example shorter and not rely on the DOM. --- _js/examples/todo.js | 46 +++++++++++++++++--------------------------- index.md | 9 +++++---- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/_js/examples/todo.js b/_js/examples/todo.js index 54ff5131..81682092 100644 --- a/_js/examples/todo.js +++ b/_js/examples/todo.js @@ -6,48 +6,38 @@ var TODO_COMPONENT = "\ /** @jsx React.DOM */\n\ var TodoList = React.createClass({\n\ render: function() {\n\ - var items = this.props.items.map(function(item) {\n\ - return
  • {item}
  • ;\n\ - });\n\ - return ;\n\ + var createItem = function(itemText) {\n\ + return
  • {itemText}
  • ;\n\ + };\n\ + return ;\n\ }\n\ });\n\ -\n\ -var TodoCreate = React.createClass({\n\ - handleSubmit: React.autoBind(function() {\n\ - var textInput = this.refs.textInput.getDOMNode();\n\ - this.props.onCreate(textInput.value);\n\ - textInput.value = '';\n\ - return false;\n\ - }),\n\ - render: function() {\n\ - return (\n\ -
    \n\ - \n\ - \n\ -
    \n\ - );\n\ - }\n\ -});\n\ -\n\ var TodoApp = React.createClass({\n\ getInitialState: function() {\n\ - return {items: []};\n\ + return {items: [], text: ''};\n\ + },\n\ + onKey: function(e) {\n\ + this.setState({text: e.target.value});\n\ + },\n\ + handleSubmit: function(e) {\n\ + e.preventDefault();\n\ + var nextItems = this.state.items.concat([this.state.text]);\n\ + var nextText = '';\n\ + this.setState({items: nextItems, text: nextText});\n\ },\n\ - onItemCreate: React.autoBind(function(value) {\n\ - this.setState({items: this.state.items.concat([value])});\n\ - }),\n\ render: function() {\n\ return (\n\
    \n\

    TODO

    \n\ \n\ - \n\ +
    \n\ + \n\ + \n\ +
    \n\
    \n\ );\n\ }\n\ });\n\ -\n\ React.renderComponent(, mountNode);\ "; diff --git a/index.md b/index.md index 9463be70..2edff303 100644 --- a/index.md +++ b/index.md @@ -56,10 +56,11 @@ id: home

    An Application

    - Using properties and state, we can put together a small Todo - application. React provides an interface to the DOM via `refs`. Although - event handlers appear to be rendered inline, they will be - collected and implemented using event delegation. + Using `props` and `state`, we can put together a small Todo application. + This example uses `state` to track the current list of items as well as + the text that the user has entered. Although event handlers appear to be + rendered inline, they will be collected and implemented using event + delegation.