redux中间件+react高阶组件

1.redux中间件

中间件提供第三方插件的模式,自定义拦截 action -> reducer 的过程。变为 action -> middlewares -> reducer。这种机制可以让我们改变数据流,实现如异步action ,action 过滤,日志输出,异常报告等功能

redux-logger:提供日志输出,每次action修改state时,都会在控制台打印出关键信息,便于开发者追溯状态。

// src/store/index.tsx
import {createStore,applyMiddleware} from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
const reducer = function(state:any,actions:any){
    if(!state){
        return {
            count:10,
            msg:'hello redux'
        }
    }

    switch(actions.type){
        case "PLUS":
            return {
                ...state,
                count:state.count+1
            }
            break;
        default:
            return state
            break
    }
}
const store =applyMiddleware(logger,thunk)(createStore)(reducer)
export default store
// 异步action,就是指action的值为函数,异步action中一般都会调用同步action,异步action不是必须要用的。

在这里插入图片描述redux-thunk:处理异步操作

// src/store/index.tsx
import React, { useEffect, useState } from 'react';
import yayJpg from '../assets/yay.jpg';
import store from '../store/index'

export default function HomePage() {
  const [user,setuser] = useState(store.getState().count)
  useEffect(()=>{
    store.subscribe(()=>{
      setuser(store.getState().count)
    })
  },[])
  // const upd = ()=>{
  //   store.dispatch({type:"PLUS"})
  // }
  const openFormAsync = () => {
    // 返回一个函数,参数是dispatch函数,函数里面包含异步操作,且一般要调用同步action
      store.dispatch((dispatch:any,getState:any)=>{
        setTimeout(() => {
          console.log(getState())
          dispatch({type:'PLUS'})
        },2000)
      })
  }
  return (
    <div>
      <h2>{user}</h2>
      <p>
        <img src={yayJpg} width="388" />
      </p>
      <p>
        To get started, edit <code>pages/index.tsx</code> and save to reload.
        <button onClick={openFormAsync}>修改</button>
      </p>
    </div>
  );
}

redux-promise:处理异步操作,actionCreator的返回值是promise

2.React高阶组件

高阶函数(Higher-order function),至少满足下列一个条件的函数

接受一个或多个函数作为输入,输出一个函数

在React中,高阶组件即接受一个或多个组件作为参数并且返回一个组件,本质也就是一个函数,并不是一个组件

高阶组件的主要功能是封装并分离组件的通用逻辑,让通用逻辑在组件间更好地被复用

高阶组件可以传递所有的props,但是不能传递ref

高阶组件能够提高代码的复用性和灵活性,在实际应用中,常常用于与核心业务无关但又在多个模块使用的功能,如权限控制、日志记录、数据校验、异常处理、统计上报等

import React from 'react'
const Gaojie = (Son: any) => {
  return function () {
    const arr = {
      one: '马新明',
      two: '小黑'
    }
    return <Son {...arr} />
  }
}

const Gao = (props: any) => {
  // console.log(props,'props');
  return (
    <div>
      {props.one}
    </div>
  )
}

const L=(props:any)=>{
  return(
    <div>
      {props.two}
    </div>
  )
}

const Fourther=()=>{
  return(
    <>
      <One/>
      <T/>
    </>
  )
}

const One = Gaojie(Gao)
const T = Gaojie(L)
export default Fourther

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值