当前位置:   article > 正文

react 父组建 调用/获取 子组建函数/参数_react const component =

react const component =
  1. import {
  2. useImperativeHandle,
  3. forwardRef,
  4. } from 'react'
  1. const Component = (props: any, ref: any) => {
  2. useImperativeHandle(ref, () => ({
  3. func1: handleChangeName ,
  4. }))
  5. const handleChangeName = async () => {
  6. console.log('name')
  7. }
  8. }
  9. export default forwardRef(Component)
  1. import { FC , useRef } from 'react'
  2. const FatherComponent : FC =()=>{
  3. const component = useRef()
  4. useEffect(() => {
  5. // component 代表子组建
  6. // func1 子组建的函数 实际调用handleChangeName
  7. component.current.func1()
  8. //console.log('name')
  9. }, [])
  10. return (
  11. <div>
  12. <Component ref={component} />
  13. </div>
  14. )
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/411785
推荐阅读
相关标签
  

闽ICP备14008679号