redux是一种对项目进行统一的状态管理机制,父子组件间通信是通过props属性值传递和子组件回调父组件提前声明好的方法,整个state只能从上到下,而没有回溯的能力,redux将所有的state集中到所有组件顶层,然后分发给每个组件自己需要的state,更好的管理状态,顶层分发状态,让React组件被动地渲染,而react hooks解决的是pure render function (渲染函数组件)拥有状态和生命周期react-redux 构成1.store单一数据源 store 由rootReducer组成,用于最外层组件import { createStore } from ‘redux’;import { Provider } from ‘react-redux’;const store = creacteStore(rootReducer);<Provider store={store}><App /></Provider>2.rootReducerrootReducer由多个reducers组成import { combineReducers } from ‘redux’;export default combineReducers({reducerA, reducerB})3.Reducer每一个reducer由 oldState(initState) 和 action组成,通过action.type 返回 newState,default 返回oldStateexport default function count (state = 0, action) { switch(action.type) { case ‘count’: return state + 1 default: return state }}4.Actionaction 可以是一个方法,用于返回对象,也可以直接是一个对象,type属性是必须的export function count () { return { type: ‘count’ }}5.conncetconnect 用于连接组件和数据import { connect } from ‘react-redux’export default connect(mapStateToProps, mapDispatchToProps)(App)6.mapStateToPropsmapStateToProps 方法用于将状态映射成属性,返回组件需要的属性const mapStateToProps = (state) => { return { count: state.count }}7.mapDispatchToPropsmapDispatchToProps 提供给组件一个属性用于触发dispatch,也就是用户触发actionconst mapDispatchToProps = (dispatch) => { return { addCount: () => dispath(someAction) // dispatch的对象是action }}react-redux注意点state = store.getState() // 获取statestate.count = state.count + 1 // state是只读的,只能通过dispatch(action)改变我写了一个完整的简单的 react-redux-count-demo 项目,也是上面演示的列子参考 Redux 简明教程和理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux? - Wang Namelos的回答 - 知乎