Passing a Ref from a Child to a Parent in React with Class Component
问题 I have an iFrame in a child component, and want to pass a ref of the iframe to the parent component so I can do a postMessage to the iframe. I am unsure how to implement forwarding refs from child to parent. Thanks! 回答1: Here's an example how can you do it const { forwardRef, useRef, useState, useEffect } = React; const Child = forwardRef((props, ref) => { const computeDataUrl = (value) => { return `data:text/html,${encodeURI(value)}` } const [data, setData] = useState(computeDataUrl('Init'))