Redux—基本使用

如图所示,即为Redux原理
redux原理图.png
一个常见的Redux组件分为以下4个部分,constant.jsaction.jsreducer.jsstore.js

  1. // 定义常量供Actions && Reducers 使用
  2. export const INCREMENT = 'increment'
  3. export const DECREMENT = 'decrement'
  1. //引入常量
  2. import {INCREMENT,DECREMENT} from './constant'
  3. //创建actions并export出去
  4. export const xxxxAction = data => ({type:INCREMENT,data})
  1. import {INCREMENT,DECREMENT} from './constant' //引入常量
  2. /*
  3. 1.该文件是用于创建一个为Count组件服务的reducer,reducer的本质就是一个函数
  4. 2.reducer函数会接到两个参数,分别为:之前的状态(preState),动作对象(action)
  5. */
  6. const initState = 0 //初始化状态
  7. export default function countReducer(preState=initState,action){
  8. // console.log(preState);
  9. //从action对象中获取:type、data
  10. const {type,data} = action
  11. //根据type决定如何加工数据
  12. switch (type) {
  13. case INCREMENT: //如果是加
  14. return preState + data
  15. case DECREMENT: //若果是减
  16. return preState - data
  17. default:
  18. return preState
  19. }
  20. }
  1. //该文件专门用于暴露一个store对象,整个应用只有一个store对象
  2. //引入createStore,专门用于创建redux中最为核心的store对象
  3. import {createStore} from 'redux'
  4. //引入为Count组件服务的reducer
  5. import countReducer from './count_reducer'
  6. //暴露store
  7. export default createStore(countReducer)

自定义组件中引入store && action执行redux

  1. //引入store,用于获取redux中保存状态
  2. import store from './store'
  3. //引入action,专门用于创建action对象
  4. import { xxxxAction } from './count_action'
  5. //具体代码--触发redux执行
  6. store.dispatch(xxxxAction(value))
  7. //创建的store还有以下几个方法可以调用
  8. //getState():获取最新的状态数据
  9. //dispatch():派发Action行为事件
  10. //subscribe():订阅Store中的状态变化

监测redux数据变化,触发重新渲染

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import App from './App'
  4. import store from './redux/store'
  5. ReactDOM.render(<App/>,document.getElementById('root'))
  6. store.subscribe(()=>{
  7. ReactDOM.render(<App/>,document.getElementById('root'))
  8. })

Redux—支持异步Action

Redux还支持异步Action,具体需要redux-thunk中间件的支持

  1. //引入createStore,专门用于创建redux中最为核心的store对象
  2. import {createStore,applyMiddleware} from 'redux'
  3. //引入为Count组件服务的reducer
  4. import countReducer from './count_reducer'
  5. //引入redux-thunk,用于支持异步action
  6. import thunk from 'redux-thunk'
  7. //暴露store
  8. export default createStore(countReducer,applyMiddleware(thunk))

React-Redux

如图所示,即为React-Redux原理
react-redux模型图.png
UI+Container组件

  1. import React, { Component } from 'react'
  2. //引入action
  3. import { increment } from '../../redux/actions/count'
  4. //引入connect用于连接UI组件与redux
  5. import {connect} from 'react-redux'
  6. //定义UI组件
  7. class Count extends Component {
  8. state = {value:''}
  9. //加法
  10. increment = ()=>{
  11. this.props.increment(value)
  12. }
  13. render() {
  14. return (
  15. <div>
  16. <div>{this.state.value}</div>
  17. <button onClick={this.increment}>+</button>
  18. </div>
  19. )
  20. }
  21. }
  22. //使用connect()()创建并暴露一个Count的容器组件
  23. export default connect(
  24. state => ({
  25. count:state.xxxxReducers,
  26. persons:state.yyyyReducers
  27. }),
  28. {increment}
  29. )(Count)
  30. //connect()()函数柯里化 第一个函数传递两个回调函数,mapStateToProps mapDispatchToProps
  31. //mapStateToProps mapDispatchToProps,会将states actions经由props传给组件

index.js(Reducers的合并)

  1. //该文件用于汇总所有的reducer为一个总的reducer
  2. //引入combineReducers,用于汇总多个reducer
  3. import {combineReducers} from 'redux'
  4. //引入为Count组件服务的reducer
  5. import xxxxReducers from './xxxx'
  6. //引入为Person组件服务的reducer
  7. import yyyyReducers from './yyyy'
  8. //汇总所有的reducer变为一个总的reducer
  9. export default combineReducers({
  10. xxxxReducers,
  11. yyyyReducers
  12. })

创建store—引入统一的reducer

  1. //引入createStore,专门用于创建redux中最为核心的store对象
  2. import {createStore,applyMiddleware} from 'redux'
  3. //引入汇总之后的reducer
  4. import reducer from './reducers/index.js'
  5. //引入redux-thunk,用于支持异步action
  6. import thunk from 'redux-thunk'
  7. //引入redux-devtools-extension
  8. import {composeWithDevTools} from 'redux-devtools-extension'
  9. //暴露store
  10. export default createStore(reducer,composeWithDevTools(applyMiddleware(thunk)))

公共外层组件—通过Provider注入store

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import App from './App'
  4. import store from './redux/store'
  5. import {Provider} from 'react-redux'
  6. ReactDOM.render(
  7. /* 此处需要用Provider包裹App,目的是让App所有的后代容器组件都能接收到store */
  8. <Provider store={store}>
  9. <App/>
  10. </Provider>,
  11. document.getElementById('root')
  12. )

关于mapStateToProps mapStateToProps
截屏2022-04-07 下午4.46.06.png

参考

  1. https://segmentfault.com/a/1190000039419540 react-redux状态管理
  2. https://imweb.io/topic/57c531bc6227a4f55a8872c2 React + Redux组件化方案
  3. https://segmentfault.com/q/1010000012919134 mapDispatchToProps
  4. http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_three_react-redux.html
  5. https://stackoverflow.com/questions/39419237/what-is-mapdispatchtoprops
  6. https://react-redux.js.org/ react-redux官网
  7. https://juejin.cn/post/7031509723190919175 redux使用