- ui组件负责view
- 容器组件负责业务逻辑
ui
~~~
import React,{Component} from 'react';
import 'antd/dist/antd.css'
import { Input, Button, List } from 'antd';
class TodoListUI extends Component {
render() {
return (
<div style={{ marginTop: "20px", marginLeft: "20px" }}>
<Input
value={this.props.inputValue}
onChange={this.props.handleChange}
style={{ width: 300, marginRight: "10px" }} />
<Button type="primary" onClick={this.props.handleClick}>添加</Button>
<List
style={{ marginTop: "10px", width: "300px" }}
bordered
dataSource={this.props.list}
renderItem={(item,index) => (<List.Item onClick={(index)=>{this.props.handleDelete(index)}}>{item}</List.Item>)}
/>
</div>
)
}
}
export default TodoListUI;
~~~
- 容器组件
~~~
import React, { Component } from 'react';
import store from './store/index';
import {getInputChangeAction,getAddItemAction,getDeleteItemAction} from './store/actionCreators';
import TodoListUI from './TodoListUI';
class App extends Component {
constructor(props) {
super(props);
this.state = store.getState();
/* 订阅store的改变,只要store改变,handleStoreChange方法就会执行 */
store.subscribe(this.handleStoreChange);
}
render() {
return (
<TodoListUI
list = {this.state.list}
handleChange = {this.handleChange}
handleClick={this.handleClick}
handleDelete={this.handleDelete}
inputValue = {this.state.inputValue}/>
)
}
handleChange=(e)=>{
let { value } = e.target;
let action = getInputChangeAction(value);
store.dispatch(action);
}
handleStoreChange=()=> {
this.setState(store.getState())
}
handleClick=()=>{
let action=getAddItemAction();
store.dispatch(action);
}
handleDelete(index){
let action=getDeleteItemAction(index);
store.dispatch(action)
}
}
export default App;
~~~
- 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.起步