## ant-design實現TodoList
[例子](https://gitee.com/chengbenchao/react-tutorial/tree/master/01ant-design)
~~~
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;
~~~
~~~
//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;
}
~~~
- react
- 第一章 React入門
- 1-1 開發環境搭建
- 1-2 循環
- 1-3 jsx語法
- 1-4 react特點
- 第二章 基本語法
- 2-1 組件
- 2-2 實現一個簡單的TodoList
- 2-2-1刪除
- 2-3 組件之間的傳值
- 2-4 子組件向父組件傳值
- 2-5 react-router實現一個簡單路由
- 2-6 ref的使用
- 2-7 setState方法
- 2-8 生命周期函數
- 2-9 react的css過渡動畫
- 2-10 react中的內聯樣式
- 2-11 事件
- 2-12 箭頭函數
- 第三章 redux
- 第一節 使用
- 1.1 action
- 1.2 實現todoList的增刪功能
- 1.3 actionTypes的拆分
- 1.4 actionCreators.js統一管理action
- 1-5 redux設計的三大原則
- 第二節 安裝Redux
- 第三節 redux進階
- 3.1 ui組件和容器組件的拆分
- 3.2無狀態組件
- 3.3 Redux-thunk中間件ajax請求數據
- 3.4redux中間件
- 3.5 redux-saga中間件
- 第四節 react-redux
- 第四章 項目啟動
- 第一節 styled-components
- 1-1 style 引入背景圖片
- 1-2 樣式組件
- ant-design
- 1.起步