Adding ref with React.cloneElement() fails on class component with functional component error
未關閉
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
In a class component with all class component children...
constructor(props) {
super(props);
this.myref = React.createRef();
}
render() {
return (
{React.cloneElement(this.props.children[0], {ref: myref})}
);
}
...fails with Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
I get that React.cloneElement() is probably returning a functional wrapper that's confusing React, but given the power of this.props.children, what is the proper way to dynamically add refs? All of the actual children are class components if that matters.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
報告提到了 React.cloneElement()、React.createRef()、類別元件和函式元件,但沒有提供任何儲存庫檔案或測試進入點。首先使用所示的元件結構重現該警告,並確定預期行為需要修正 React,還是需要釐清文件;完成表示得到一致同意且可重現的結果,以及一項已記錄或經過測試的解決方案。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- frontend
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 25/100