react Todo List

前端之家收集整理的这篇文章主要介绍了react Todo List前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import App from './App';
  4. import './index.css';
  5.  
  6. var TodoList = React.createClass({
  7. getInitialState: function() {
  8. return {items: []};
  9. },handleAdd: function() {
  10. var newItem = this.refs.newItem.value;
  11. if (newItem.length == 0) {
  12. return;
  13. }
  14. this.refs.newItem.value = "";
  15. this.refs.newItem.focus();
  16. this.setState({items: this.state.items.concat(newItem)});
  17. },render: function() {
  18. return (
  19. <div>
  20. <h2>Todo List</h2>
  21. <ul>
  22. {
  23. this.state.items.map(function(item) {
  24. return <li>{item}</li>
  25. })
  26. }
  27. </ul>
  28. <input type="text" ref="newItem" />
  29. <input type="button"
  30. value={'Add #' + (this.state.items.length + 1)}
  31. onClick={this.handleAdd}>
  32. </input>
  33. </div>
  34. )
  35. },});
  36.  
  37. var TodoApp = React.createClass({
  38. render: function() {
  39. return (
  40. <TodoList />
  41. );
  42. }
  43. })
  44.  
  45. ReactDOM.render(
  46. <TodoApp />,document.getElementById('root')
  47. );

猜你在找的React相关文章