-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (57 loc) · 1.72 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// var TodoList = (props) => {
// // This function will be called when the first `<li>` below is clicked on
// // Notice that event handling functions receive an `event` object
// // We want to define it where it has access to `props`
// var onListItemClick = (event) => {
// console.log('I got clicked');
// };
// // Because we used curly braces with this arrow function
// // we have to write an explicit `return` statement
// return (
// <ul>
// <li onClick={onListItemClick}>{props.todos[0]}</li>
// <li>{props.todos[1]}</li>
// <li>{props.todos[2]}</li>
// </ul>
// );
// }
var App = () => (
<div>
<h2>My Todo List</h2>
<TodoList todos={['Learn React', 'Crush Recast.ly', 'Maybe sleep']}/> // Here we are creating an instance of the `TodoList` component
</div>
);
// A class component can be defined as an ES6 class
// that extends the base Component class included in the React library
class TodoListItem extends React.Component {
constructor(props) {
super(props);
this.state = {
done: false
};
}
onListItemClick() {
this.setState({
done: !this.state.done
})
}
render() {
var style = {
textDecoration: this.state.done ? 'line-through' : 'none'
};
return (
<li style={style} onClick={this.onListItemClick.bind(this)}>{this.props.todo}</li>
)
}
}
// Update our `TodoList` to use the new `TodoListItem` component
// This can still be a stateless function component!
var TodoList = (props) => (
<ul>
{props.todos.map(todo =>
<TodoListItem todo={todo} />
)}
</ul>
);
ReactDOM.render(<App />, document.getElementById("app"));
// ReactDOM.render(<GroceryList />, document.getElementById("app"));