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