# 1.2 實現todoList的增刪功能
## ant-design實現TodoList
[例子](https://gitee.com/chengbenchao/react-tutorial/tree/master/01ant-design)
前提要安裝antd和redux
~~~
//app.js
import React, { Component } from 'react';
import 'antd/dist/antd.css'
import { Input, Button, List } from 'antd';
import store from './store/index';
class App extends Component {
constructor(props) {
super(props);
this.state = store.getState();
/* 訂閱store的改變,只要store改變,handleStoreChange方法就會執行 */
store.subscribe(this.handleStoreChange);
}
render() {
return (
<div style={{ marginTop: "20px", marginLeft: "20px" }}>
<Input
value={this.state.inputValue}
onChange={this.handleChange}
style={{ width: 300, marginRight: "10px" }} />
<Button type="primary" onClick={this.handleClick.bind(this)}>添加</Button>
<List
style={{ marginTop: "10px", width: "300px" }}
bordered
dataSource={this.state.list}
renderItem={(item,index) => (<List.Item onClick={this.handleDelete.bind(this,index)}>{item}</List.Item>)}
/>
</div>
)
}
handleChange=(e)=>{
let { value } = e.target;
let action = {
type: 'change_input_value',
value,
}
store.dispatch(action);
}
handleStoreChange=()=> {
this.setState(store.getState())
}
handleClick=()=>{
let action={
type:'add_todo_item'
}
store.dispatch(action);
}
handleDelete(index){
let action={
type:'delete_item',
index
}
store.dispatch(action)
}
}
export default App;
~~~
~~~
//src>store>reducer.js
const defaultState={
inputValue:'hello',
list:[1,2,3]
};
/* state指store中的數據 */
/* reducer可以接收state,但不能修改state */
export default (state=defaultState,action)=>{
/* ruducer拿到之前的數據,和action中傳遞過來的數據作比對 */
if(action.type==='change_input_value'){
const newState = {...state};
newState.inputValue = action.value;
return newState;
}
if(action.type==="add_todo_item"){
const newState ={...state};
newState.list.push(newState.inputValue)
newState.inputValue=""
return newState
}
if(action.type==="delete_item"){
const newState = {...state};
newState.list.splice(action.index,1);
return newState;
}
return state;
}
~~~
~~~
~~~
//src>store>index.js
import { createStore} from 'redux';
import reducer from './reducer';
const store = createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
export default store;
~~~
~~~
- 第一章 起步
- 第1節 創建react項目
- 第2節 hello world
- 第3節 數據綁定+事件處理
- 3.1 for循環事件處理中的傳參寫法、條件渲染
- 第4章 點擊切換文字
- 第5章 使用html寫react
- 第二章 運用
- 第1節 循環
- 第2節 實現一個簡單的TodoList
- 第2.1節 刪除
- 第3節 父子組件傳參
- 1. 父組件向子組件傳參
- 2. 子組件向父組件傳參
- 第4節 react-router實現一個簡單路由
- 第5節 生命周期
- 第6節 取數據
- 第 7節 獲取dom節點
- 第8節 雙向數據綁定
- 第三章 redux
- 第1節 介紹
- 第2節 安裝redux
- 第3節 使用
- 3.1 action
- 3.2 使用redux實現 todolist
- 第4節封裝redux中的action
- 第5節 redux-thunk中間件
- 5.1介紹
- 5.2使用
- 第四章 ant-design前端ui
- 第一節 安裝
- 第2節 使用
- 2.1 ant-design實現todoList增刪功能
- 第3節 使用整理
- 第五章 vue和react的比較
- 第六章 dva.js輕量級應用框架
- 第1節 介紹
- 第2節 安裝dva
- 第3節 頁面跳轉
- 1. 事件路由跳轉
- 2. 通過路由跳轉
- 第4節 組件之間通信
- 1. 父組件向子組件傳參
- 2. 子組件向父組件傳參
- 第5節 事件處理
- 第6節 發送請求
- 1. 通過路由判斷頁面渲染數據
- 2. 通過事件發送請求
- 第7節 運用
- 1. TodoList
- 1.添加數據
- 1.2輸入框敲回車觸發事件
- 2.刪除數據
- 3. 總結
- 第8節 配合antd使用
- 1. 引入antd
- 2.dva 使用antd注意事項
- 3. 知識點整理
- 第七章 dva后臺項目實戰
- 第1節 登錄加密
- 1.具體實現
- 第2節 知識點
- 第3節 樹結構
- 第八章 react新特性 hooks
- 第1節 hooks介紹
- 第2節 useState的使用
- 第3節 useEffect的使用
- 第4節 dva+antd+hooks企業后臺項目開發流程
- 第 5節 hooks 使用
- 運用
- 第6節 hook整理
- 第7節 react memo
- 第九章 react中使用Echarts
- 知識點
- react中使用mobx
- 知識點
- react中使用rem
- 遞歸實現目錄數
- react使用圖表
- react 同步更新策略
- antd tree默認展開無效
- ts中lint修復篇
- React-query方案
- 高階組件