How does React Hooks realize father component to call child the method of package?

At 2 o\’clock premise:

  1. Function type component, use React Hooks
  2. Child package derives method, the component that offer father is in appropriate opportunity (want when when) call

The method that I think of exclusively is use Ref, following, but this also too TM is ugly, appeal old man people

type ChildFunc = () => number

// eslint-disable-next-line react/display-name
const Child = React.forwardRef((props, ref) => {

  // eslint-disable-next-line no-param-reassign
  (ref as React.MutableRefObject<ChildFunc | undefined>).current = () => 5

  return <>I am child</>
})

// Parent
function Parent() {
  const funcRef = useRef<ChildFunc>()

  const theRightTime = new Promise((resolve) => {
    setTimeout(resolve, 1000)
  })

  useEffect(() => {
    theRightTime.then(() => {
      if (funcRef.current) {
        console.log(funcRef.current())
      }
    })
  }, [funcRef, theRightTime])

  return <Child ref={funcRef} />
}

Carry one mouth incidentally, coping of React official net has a \” scroll \”- – the life of \” black also is a life. Support just judicatory proposal. Support just judicatory proposal.. – – but everybody is OK switch language looks, seem to have simplified Chinese and English only, traditional Chinese, Japanese do not have (so multiligualism, I also did not break up one by one, but several when I looked are done not have)

Related Posts

15 thoughts on “How does React Hooks realize father component to call child the method of package?

  1. The statement of this motion is like the author that is Django Rest Api to initiate, they are direct give official documentation changed supportive statement. React hangs a banner to also be very large support only.

  2. Is React should individual data flows do you ask this to should put Function to father component directly or Util?

    I am dish bird ha

  3. Passing Ref was to pass an OnLoad function to give substantially child package. Child this function is moved after package Mount, and pass itself go in.
    The issue that component of your this stature offers a means is a truth.

  4. @fuermosi777 I am other function and style are written, did not want to change ah T.T
    This TheRightTime just makes clear @crs0910, in appropriate opportunity, spark by father component just, still say father component needs to be called in appropriate opportunity substantially child the method of package ah
    @ps4512 follows Callback should it doesn’t matter concerns
    @dartabe puts father component to do not have these problems really, it is some self only, can read the content of my Append
    @WittBulter I do not know this Hook really however, go up pose, acknowledgment (but tell the truth, ” of such grace of insufficient still ” . . . ..
    No matter @ChefIsAwesome is Ref it may not be a bad idea, global variable it may not be a bad idea, will although can come true constrainedly,look at present, but a little ugly, of 6L should be at present first-rate

  5. BlackLivesmatter is troubled by badlier than epidemic situation now, not only it is the United States, europe also begins to chare grabbed.

  6. The method is very much for instance
    Father component changes child the Props of package next child package comes to this Props as Dependency spark behavior

  7. I am handle component is enclosed into an UseXXX() commonly, next Return Dom and the method that need to callConst UseChild=()=>{Return {
    }
    }

  8. I am handle component is enclosed into an UseXXX() commonly, next Return Dom and the method that need to callConst UseChild=()=>{Return {Dom: <child />Methods:{}
    }
    }

  9. This pours @LeoooY is a good method, how cannot I think of at that time. . . I already primary reason child package pilot attribute moves for the most part father component. . .

  10. Is @KuroNekoFan likely not quite travel? Say to hope child package exposes a function, because function is used,go to the lavatory namely and semantic and clear, is with attribute the likelihood in are this two sides not quite right?

  11. @xiaoming1992 alleged ‘ child package exposes a function ‘ , react does not hope developer is done soHttps://zh-hans.reactjs.org/docs/hooks-reference.html#useimperativehandle government documentation also said, ought to avoid
    Use Props control besides what I say, still can use incident, state government and so on

Leave a Reply

Your email address will not be published. Required fields are marked *