react 父组件调用自组件方法,forwardRef与connect联合使用

it2025-09-02  4

简单的调用网上很多,主要这两个联合

https://www.jianshu.com/p/9a41f3dc8468

父组件

let Draw = (props) => { let {headername,addCancelTools} = props; let [selIndex,setIndex] = useState(0); const childRef=useRef(); console.log(childRef) if(childRef.current){ childRef.current.getSetFunction()([]) } return ( <div id="bosgeo-draw" className="popup"> <Header headername={headername} addCancelTools={addCancelTools}/> <div className="firstWrapper"> <DrawMode selIndex={selIndex} setIndex={setIndex}/> <DrawOptions selIndex={selIndex} ref={childRef}/> <DrawRecord selIndex={selIndex} /> </div> </div> ) }

子组件

DrawOptions =connect(state =>({drawList:state.drawlist})) (DrawOptions) /*eslint-disable*/ export default forwardRef( (props,ref) => <DrawOptions {...props} refInstance={ref}/>)

 

最新回复(0)