import React, { Component } from "react";
import store from "./store";
import TodoListUI from './TodoListUI';
class TodoList extends Component {
constructor(props) {
super(props);
this.state = store.getState()
this.handleInputChange = this.handleInputChange.bind(this)
this.handleStoreChange = this.handleStoreChange.bind(this)
this.handleBtnClick = this.handleBtnClick.bind(this)
this.handleItemDelete = this.handleItemDelete.bind(this)
// 自动订阅store,监听store的内容
store.subscribe(this.handleStoreChange);
}
render() {
// 拆分UI组件
return <TodoListUI
inputValue={this.state.inputValue}
handleInputChange={this.handleInputChange}
handleBtnClick={this.handleBtnClick}
list={this.state.list}
handleItemDelete={this.handleItemDelete}
/>
}
handleInputChange(e) {
const value = e.target.value;
const action = {
type: 'change_input_value',
value: value
}
store.dispatch(action)
}
handleStoreChange () {
this.setState(store.getState())
}
handleBtnClick() {
const value = this.state.inputValue
const action = {
type: 'change_child_value',
value: value
}
store.dispatch(action);
}
handleItemDelete(index) {
const action = {
type: 'del_child_value',
index: index
}
console.log(action)
store.dispatch(action)
}
}
export default TodoList