父組件:const Parent = (props) => { const ref = useRef(); return <Child ref={ref} />}和孩子:const Child = forwardRef((props, ref) => { return <button ref={ref} ...>click</button>})如果我想傳遞更多的道具怎么Child辦ref?我搜索了文檔和教程,但一無所獲;通過反復試驗,我想這會起作用:// in parent<Child onClick={...} prop1={...} prop2={...} ref={ref} />然后在 中,Child我可以從 中獲取這些道具 ( onClick,, ) 。prop1prop2props這就是我需要做的嗎?通過把ref作為最后一個道具傳遞給孩子?如果我有多個按鈕Child需要一個怎么辦ref?
React forwardRef: ref 和其他道具
哈士奇WWW
2023-03-18 11:20:03