首页 > 其他分享 >实现 React-redux(一) connect 和 mapStateToProps

实现 React-redux(一) connect 和 mapStateToProps

时间:2024-03-18 09:45:57浏览次数:25  
标签:themeColor const mapStateToProps React state import return redux store

1.结合 context 和 store

  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types'
  3. function createStore (reducer) {
  4. let state = null
  5. const listeners = []
  6. const subscribe = (listener) => listeners.push(listener)
  7. const getState = () => state
  8. const dispatch = (action) => {
  9. state = reducer(state, action)
  10. listeners.forEach((listener) => listener())
  11. }
  12. dispatch({}) // 初始化 state
  13. return { getState, dispatch, subscribe }
  14. }
  15. const themeReducer = (state, action) => {
  16. if (!state) return {
  17. themeColor: 'red'
  18. }
  19. switch (action.type) {
  20. case 'CHANGE_COLOR':
  21. return { ...state, themeColor: action.themeColor }
  22. default:
  23. return state
  24. }
  25. }
  26. const store = createStore(themeReducer)
  27. class Header extends Component {
  28. static contextTypes = {
  29. store: PropTypes.object
  30. }
  31. constructor () {
  32. super()
  33. this.state = { themeColor: '' }
  34. }
  35. componentWillMount () {
  36. const { store } = this.context
  37. this._updateThemeColor()
  38. store.subscribe(() => this._updateThemeColor())
  39. }
  40. _updateThemeColor () {
  41. const { store } = this.context
  42. const state = store.getState()
  43. this.setState({ themeColor: state.themeColor })
  44. }
  45. render () {
  46. return (
  47. <div>
  48. <h1 style={{ color: this.state.themeColor }}></h1>
  49. <ThemeSwitch />
  50. </div>
  51. )
  52. }
  53. }
  54. class ThemeSwitch extends Component {
  55. static contextTypes = {
  56. store: PropTypes.object
  57. }
  58. // dispatch action 去改变颜色
  59. handleSwitchColor (color) {
  60. const { store } = this.context
  61. store.dispatch({
  62. type: 'CHANGE_COLOR',
  63. themeColor: color
  64. })
  65. }
  66. render () {
  67. return (
  68. <div>
  69. <button onClick={this.handleSwitchColor.bind(this, 'red')}>Red</button>
  70. <button onClick={this.handleSwitchColor.bind(this, 'blue')}>Blue</button>
  71. </div>
  72. )
  73. }
  74. }
  75. class App extends Component {
  76. static childContextTypes = {
  77. store: PropTypes.object
  78. }
  79. getChildContext () {
  80. return { store }
  81. }
  82. render() {
  83. return (
  84. <div>
  85. <Header />
  86. </div>
  87. );
  88. }
  89. }
  90. export default App;

2.connect 和 mapStateToProps

App.js:

  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types'
  3. import Header from './Header'
  4. function createStore (reducer) {
  5. let state = null
  6. const listeners = []
  7. const subscribe = (listener) => listeners.push(listener)
  8. const getState = () => state
  9. const dispatch = (action) => {
  10. state = reducer(state, action)
  11. listeners.forEach((listener) => listener())
  12. }
  13. dispatch({}) // 初始化 state
  14. return { getState, dispatch, subscribe }
  15. }
  16. const themeReducer = (state, action) => {
  17. if (!state) return {
  18. themeColor: 'red'
  19. }
  20. switch (action.type) {
  21. case 'CHANGE_COLOR':
  22. return { ...state, themeColor: action.themeColor }
  23. default:
  24. return state
  25. }
  26. }
  27. const store = createStore(themeReducer)
  28. class App extends Component {
  29. static childContextTypes = {
  30. store: PropTypes.object
  31. }
  32. getChildContext () {
  33. return { store }
  34. }
  35. render() {
  36. return (
  37. <div>
  38. <Header />
  39. </div>
  40. );
  41. }
  42. }
  43. export default App;

Header.js:

  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types'
  3. import { connect } from './react-redux'
  4. import ThemeSwitch from './ThemeSwitch'
  5. class Header extends Component {
  6. static propTypes = {
  7. themeColor: PropTypes.string
  8. }
  9. render () {
  10. return (
  11. <div>
  12. <h1 style={{ color: this.props.themeColor }}></h1>
  13. <ThemeSwitch/>
  14. </div>
  15. )
  16. }
  17. }
  18. const mapStateToProps = (state) => {
  19. return {
  20. themeColor: state.themeColor
  21. }
  22. }
  23. Header = connect(mapStateToProps)(Header)
  24. export default Header

react-redux.js:

  1. import React, { Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. export const connect = (mapStateToProps) => (WrappedComponent) => {
  4. class Connect extends Component {
  5. static contextTypes = {
  6. store: PropTypes.object
  7. }
  8. constructor () {
  9. super()
  10. this.state = { allProps: {} }
  11. }
  12. componentWillMount () {
  13. const { store } = this.context
  14. this._updateProps()
  15. store.subscribe(() => this._updateProps())
  16. }
  17. _updateProps () {
  18. const { store } = this.context
  19. let stateProps = mapStateToProps(store.getState(), this.props) // 额外传入 props,让获取数据更加灵活方便
  20. this.setState({
  21. allProps: { // 整合普通的 props 和从 state 生成的 props
  22. ...stateProps,
  23. ...this.props
  24. }
  25. })
  26. }
  27. render () {
  28. return <WrappedComponent {...this.state.allProps} />
  29. }
  30. }
  31. return Connect
  32. }

ThemeSwitch.js

  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types'
  3. class ThemeSwitch extends Component {
  4. static contextTypes = {
  5. store: PropTypes.object
  6. }
  7. // dispatch action 去改变颜色
  8. handleSwitchColor (color) {
  9. const { store } = this.context
  10. store.dispatch({
  11. type: 'CHANGE_COLOR',
  12. themeColor: color
  13. })
  14. }
  15. render () {
  16. return (
  17. <div>
  18. <button onClick={this.handleSwitchColor.bind(this, 'red')}>Red</button>
  19. <button onClick={this.handleSwitchColor.bind(this, 'blue')}>Blue</button>
  20. </div>
  21. )
  22. }
  23. }
  24. export default ThemeSwitch

 

 

 

 

 

 

标签:themeColor,const,mapStateToProps,React,state,import,return,redux,store
From: https://www.cnblogs.com/mounterLove/p/18075903

相关文章

  • 实现 React-redux(三) Provider
    react-redux.js:importReact,{Component}from'react'importPropTypesfrom'prop-types'exportconstconnect=(mapStateToProps,mapDispatchToProps)=>(WrappedComponent)=>{classConnectextendsComponent{staticconte......
  • 实现 React-redux(二) mapDispatchToProps
     App.js:importReact,{Component}from'react';importPropTypesfrom'prop-types'importHeaderfrom'./Header'functioncreateStore(reducer){letstate=nullconstlisteners=[]constsubscribe=(listener)=>l......
  • 可编辑表格中的两个列分别是用react-hook-form 和antd的inputNumber实现的,需要在开始
    可编辑表格中的两个列分别是用react-hook-form和antd的inputNumber实现的,需要在开始时间的列输入后失焦时,或者按enter键,鼠标聚焦到下一列,即结束时间,该如何设置在React项目中,要实现在一个可编辑表格中,当开始时间列输入后失焦或按下Enter键时,自动将焦点切换至结束时间列,你可以结合......
  • react中setState是同步的还是异步的
    首先说一下setState是同步的还是异步的?1.解读setState工作流 接下来我们就沿着这个流程,逐个在源码中对号入座。首先是setState入口函数:ReactComponent.prototype.setState=function(partialState,callback){this.updater.enqueueSetState(this,partialSta......
  • 【原创】三十分钟实时数据可视化网站前后端教程 Scrapy + Django + React 保姆级教程
    这个本来是想做视频的,所以是以讲稿的形式写的。最后没做视频,但是觉得这篇文还是值得记录一下。真的要多记录,不然一些不常用的东西即使做过几个月又有点陌生了。文章目录爬虫SCRAPYxpath后端DJANGO前端REACTHello大家好这里是小鱼,最近我做了一个前后端分离的实......
  • React — zustand状态管理工具
    zustand是一个用于状态管理的简单而强大的库,它可以与React一起使用。它提供了一种简单的方式来管理组件的状态,并且遵循了ReactHooks的使用规范。使用zustand可以方便地创建和共享状态,同时还能够实现状态的订阅和更新。通过zustand,你可以创建自定义的状态钩子,并在组件中......
  • 前端React篇之React setState 调用的原理、React setState 调用之后发生了什么?是同步
    目录ReactsetState调用的原理ReactsetState调用之后发生了什么?是同步还是异步?ReactsetState调用之后发生了什么?setState是同步还是异步的ReactsetState调用的原理在React中,setState方法是用于更新组件状态的重要方法。当setState被调用时,React会对组件进......
  • 除了Redux能不能使用zustand作为局部作用域的数据统一存储
    当然可以。Zustand作为一个轻量级的状态管理库,非常适合用来作为局部作用域的数据统一存储方案。相较于Redux,它更注重简洁性和易用性,并且充分利用了ReactHooks的特性。在使用Zustand时,你可以创建多个独立的store来管理不同组件或模块的本地状态。每个store都是自包含......
  • React — Class类组件
    一、Class类组件基础模板import'./App.css';import{Component}from'react'classCounterextendsComponent{//编写组件的逻辑代码//1.状态变量事件回调UI//2.定义状态变量state={count:0}setCount=()=>{this.setState({c......
  • 使用useContext和useReducer实现类似于redux的简单状态管理
    useContext和useReducer的联合用法(实现多组件多状态管理)useReduceruseReducer(reducer,initialArg,init?)参数reducer:(state:S,action:A)=>newState:S;用于更新state的纯函数。参数为state和action,返回值是更新后的state。state与action可以是任意合法值。initi......