用ts 跟了一遍TodoList.
先总结下基本的收获
- Action - 必须带个type字段. 是用来表述发生了什么和发生变化的数据
{ type: ADD_TODO, id: nextTodoId++, text };
- Action Creator - 就是对Action做个封装的function
export let addTodo = (text: string) => { return { type: ADD_TODO, id: nextTodoId++, text }; }
- Reducer - 根据action不同对state的更新
const todo = (state, action) => { |
- CombineReducer - 对多个reducer的整合,store只接受一个参数
export const todoAppReducer = combineReducers({
visibilityFilter,
todos
})
- Store
- Holds application state;
- Allows access to state via getState();
- Allows state to be updated via dispatch(action);
- Registers listeners via subscribe(listener);
- Handles unregistering of listeners via the function returned by subscribe(listener).
- It’s important to note that you’ll only have a single store in a Redux application.
- When you want to split your data handling logic, you’ll use reducer composition instead of many stores.
组建分类:
- Container
- Component
Component其实就是无状态组建,所有的绑定都是根据props来的
import * as React from 'react'; |
Container是会根据state变化做出重新render的容器组建
import * as React from 'react'; |
React-Redux
connect
就一个作用把state里的字段自动赋值给组建的props
两个主要方法:
const mapStateToProps: IMapStateToProps = (state, ownProps) => { |
用来作用在container 组建上
@connect(mapStateToProps, mapDispatchToProps) |
目前ts还不支持 object spread 语法,所以写起来郁闷点.
还有个坑就是注意,如果props.childer 容易忘
Provider
就一个作用减少自己每个组建传递store的麻烦.只能包裹一个根组建
export class App extends React.Component<any, any> { |
Source Code: https://github.com/dreambo8563/react-Tutorial/tree/master/app