Last active
August 30, 2016 03:28
-
-
Save erspark2002/eaf05f3e064accd9ec54f4b1aed4836a to your computer and use it in GitHub Desktop.
Simple React Todo Application using CDN resources for babel/ES6 - VERSION 2 - Multiple Components
This file contains 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
<!DOCTYPE html> | |
<!-- | |
NOTES: | |
multiple components | |
uses refs | |
no state | |
--> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://fb.me/react-with-addons-15.0.0.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.min.js"></script> | |
<script src="https://fb.me/react-dom-0.14.0.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script> | |
<title>React Template with CDN Resources - JSX</title> | |
</head> | |
<body> | |
<div id="app"></div> | |
<script type="text/babel"> | |
console.log('start'); | |
const Component = React.Component; | |
let count = 0; | |
let todos = [{desc: 'aaa', id: count++}]; | |
let todoList; | |
class Todo extends Component { | |
render() { | |
return ( | |
<div> | |
<TodoItem todoList={todoList}/> | |
<TodoList todos={todos} ref={el=>{todoList=el}}/> | |
</div> | |
); | |
} | |
}; | |
class TodoItem extends Component { | |
render() { | |
let input; | |
return ( | |
<div> | |
<input ref={el=>(input=el)}/> | |
<button onClick={()=>{ | |
console.log('#input:', input.value); | |
todos.push({ | |
desc: input.value, | |
id: count++ | |
}); | |
console.log('#todos:', todos); | |
todoList.forceUpdate(); | |
}}>Add</button> | |
</div> | |
); | |
} | |
} | |
class TodoList extends Component { | |
render() { | |
console.log('--> TodoList todos:', todos); | |
return ( | |
<ul> | |
{todos.map(todo => ( | |
<li key={todo.id}> | |
{todo.desc} | |
</li> | |
))} | |
</ul> | |
); | |
} | |
} | |
ReactDOM.render(<Todo />, document.getElementById('app')); | |
console.log('done'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version 3 replaces input ref={} with internal component state: https://gist.github.com/erspark2002/af5523f505393053b331db60cac41a4e