-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.react.js
More file actions
82 lines (71 loc) · 2.07 KB
/
index.react.js
File metadata and controls
82 lines (71 loc) · 2.07 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {createStore, combineReducers} from 'redux-lite';
import todos from './reducers/todos';
import {addTodo, toggleTodo, removeTodo} from './actions/todos';
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
let store = createStore(combineReducers({
todos: todos
}));
store.dispatch({
type: 'FIRST'
});
function Todo({text, onClick, completed, onRemoveClick}) {
const style = {textDecoration: completed ? 'line-through': ''};
return (<li onClick={onClick} style={style}>
{text} <button onClick={onRemoveClick}>x</button>
</li>);
}
function TodosList({todos, onClick, onRemoveClick}) {
return (
<ul>
{todos.map((todo) => {
return <Todo key={todo.id} text={todo.text}
completed={todo.completed}
onClick={ () => onClick(todo.id)}
onRemoveClick={() => onRemoveClick(todo.id)}/>;
})}
</ul>
);
}
let TodosContainer = React.createClass({
onTodoRemoveClick(id) {
store.dispatch((removeTodo(id)));
},
onTodoClick(id) {
store.dispatch(toggleTodo(id));
},
getInitialState() {
return this.props.store.getState();
},
componentDidMount() {
this.props.store.subscribe(() => {
this.setState(this.props.store.getState());
});
},
render() {
return <TodosList todos={this.state.todos}
onClick={this.onTodoClick}
onRemoveClick={this.onTodoRemoveClick}/>;
}
});
let AddTodo = () => {
let input;
return (
<div>
<input ref={node => {input = node} } />
<button onClick={() => {
store.dispatch(addTodo(input.value));
input.value = '';
}}>Add Todo</button>
</div>
);
};
let TodoApp = React.createClass({
render: () => (
<div>
<AddTodo />
<TodosContainer store={store}/>
</div>
)
});
ReactDOM.render(<TodoApp />, document.querySelector('.main-js'));