-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
92 lines (77 loc) · 1.92 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { createStore, bindActionCreators } from 'redux'
import { connect, Provider } from 'react-redux'
const ADD_TODO = 'ADD_TODO'
const REMOVE_TODO = 'REMOVE_TODO'
const TodoActions = {
addTodo(text) {
return {
type: ADD_TODO,
text
}
},
removeTodo(id) {
return {
type: REMOVE_TODO,
id
}
}
}
const initialState = {
todos: ['make a sandwich', 'kiss a grenouille']
}
const todoReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_TODO:
return {
todos: [...state.todos, action.text]
}
case REMOVE_TODO:
return {
todos: state.todos.filter((todo, i) => i !== action.id)
}
default:
return state
}
}
// @connect(state => ({todos: state.todos})) NOT COMPATIBLE WITH BABEL 6 YET
class Todo extends Component {
onAddTodo = (e) => {
const text = e.target.value.trim()
if (!text || e.keyCode !== 13) return
this.props.actions.addTodo(text)
e.target.value = ''
};
onButtonClick = () => {
this.props.actions.addTodo(this.refs.input.value)
this.refs.input.value = ''
};
render() {
const { todos } = this.props
const { removeTodo } = this.props.actions
return (
<div>
<ul>
{todos.map((todo, i) =>
<li key={i}>{todo}
<button onClick={removeTodo.bind(this, i)}>X</button>
</li>
)}
</ul>
<input ref="input" onKeyDown={this.onAddTodo} placeholder="Input text" />
<button onClick={this.onButtonClick}>Add todo</button>
</div>
)
}
}
const TodoApp = connect(
state => (state),
dispatch => ({actions: bindActionCreators(TodoActions, dispatch)})
)(Todo)
const store = createStore(todoReducer)
ReactDOM.render(
<Provider store={store}>
<TodoApp />
</Provider>,
document.getElementById('app'));