先使用 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 (
<div>This is a demo</div>
)
}
let Conponent = connect(mapStateToProps, null)(DemoComponent)
// 注意:ref要更换名字,防止覆盖
export default forwardRef( (props,ref) => <Component {...props} refInstance={ref} />)
这样就可以同时使用两个高阶组件了
网友评论