是一个数据仓库,一个应用中store是惟一的,它里面封装了state状态,当用户想访问state的时候,只能经过store.getState来取得state对象。css
action描述了一个更新state的动做,它是一个对象,其中type属性是必须有的,reducer会根据type来指定某动做和要修改的值。react
{
type: FETCH_POSTS,
payload: posts // 数据源
}
复制代码
dispatch是一个方法,它用于派发一个action,这是惟一的可以修改state的方法。git
fetch("https://jsonplaceholder.typicode.com/posts")
.then(
posts => dispatch({
type: FETCH_POSTS,
payload: posts
})
)
复制代码
reducer是更新state的核心,它里面已经封装好了更新state的逻辑,接受两个参数,state和action,将新值更新到旧的state对象上返回。github
// 根据action的type,返回不一样数据
switch (action.type) {
case FETCH_POSTS:
return {
...state,
items: action.payload
};
default:
return state;
}
复制代码
屏幕快照 2019-09-02 下午2.54.02.pngjson
import { Provider } from 'react-redux';
import { store } from './redux/store';
// Provider包裹以后,全部的组件均可以拿到store库中数据
<Provider store={store}>
// 项目中经常使用Provider包裹最底层父组件,以保证全部组件都能共享数据
<Index />
</Provider>
复制代码
import {applyMiddleware, createStore} from "redux";
import rootReducer from './reducers/index.js';
import thunk from 'redux-thunk'; // thunk做用是异步的分发action
const middleware = [thunk]; // 中间件
const initialState = {};
复制代码
// applyMiddleware将全部中间件组成一个数组并依次执行
export const store = createStore(
rootReducer,
initialState,
applyMiddleware(...middleware),
);
复制代码
// 分发操做
export const fetchPosts = () => dispatch => {
fetch("https://jsonplaceholder.typicode.com/posts")
.then(res => res.json())
.then(posts => dispatch({
type: 'FETCH_POSTS', // type是必需要有的
payload: posts // 有效数据
}));
};
复制代码
const initialState = {
items: [],
item: {}
};
// reducer接受两个参数,第一个是state,第二个是action
export default function (state = initialState, action) {
switch (action.type) {
case FETCH_POSTS:
return {
...state,
items: action.payload
};
default:
return state;
}
}
复制代码
import {connect} from 'react-redux';
import {fetchPosts} from "../redux/actions/postActions";
@connect(state => {
let {items, item} = state.posts;
return {
items,
item
}
},{fetchPosts})
复制代码
componentDidMount () {
this.props.fetchPosts();
}
componentWillReceiveProps (nextProps) {
if(nextProps.item){
this.props.items.unshift(nextProps.item);
}
}
复制代码
yarn add babel-plugin-import --save
// 在package.json中添加
"babel": {
"presets": [
"react-app"
],
"plugins": [
[
"import",
{
"libraryName": "antd",
"style": "css"
}
]
]
}
复制代码
// 将最新的state映射到当前组件,经过mapStateToProps订阅store
const mapStateToProps = state => ({
posts: state.posts.items,
});
// 将当前组件Posts和action进行连接
// connect接受两个参数,第二个参数里面能够包含多个方法
export default connect(mapStateToProps,{fetchPosts})(Posts)
复制代码
yarn add babel-plugin-transform-decorators-legacy
// 可省略如上步骤,在头部引入所需state和action,而后直接导出组件
@connect(state => {
let {
items,
item
} = state.posts;
return {
items,
item
}
},{fetchPosts}),
export default Posts;
复制代码
刚刚加入掘金社区,欢迎提出宝贵的意见,一块儿进步学习。数组