React.forwardRef和connect的联合使用

先使用 React.forwardRef再使用 connect 包一层会使 ref属性漏掉,导致 内部实例无法传到外部;

import React, { useState, useEffect, forwardRef, useImperativeHandle } from 'react'
import { connect, useSelector } from 'react-redux'

const mapStateToProps = state => {
  return {
      pxxInfo: state.getIn(['pxx', 'pxxInfo'])
  }
}

function DemoComponent (props) {
  let { productId, nextStep, productHeadInfo, refInstance } = props

  useImperativeHandle(refInstance, () => ({
    onFinish: subNextConFirm
  }), [requiredList])

  return (
     
This is a demo
) } let Component = connect(mapStateToProps, null)(DemoComponent) // 注意:ref要更换名字,防止覆盖 export default forwardRef( (props,ref) => )

这样就可以同时使用两个高阶组件了

你可能感兴趣的:(React.forwardRef和connect的联合使用)