reactjs - 反应原生:试图分配给只读属性
问题描述
我通过在 React Native 中调用 React.createRef 创建一个 React ref。然后我将它分配给一个参考。我收到错误:尝试分配给只读属性
export default class List extends PureComponent<Props, object> {
private flatListRef: React.RefObject<FlatList<any>>;
constructor(props) {
super(props);
this.flatListRef = React.createRef();
}
render() {
return (
/.../
<FlatList ref={this.flatListRef}></FlatList>
)
}
}
但是当我使用回调方式分配反应参考时,一切正常。
<FlatList ref={ele => { this.flatListRef = ele }}></FlatList>
我不知道这两种方式有什么区别
解决方案
the ref
property in React is expecting a function and it's called immediately after the component is mounted. You can do other things besides setting a reference.
https://zhenyong.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute
推荐阅读
- excel - Microsoft Graph API - Excel:修改表格范围
- javascript - 如何将对象键转换为嵌套对象
- android - 我可以在没有库部分 recyclerview 的情况下使用部分 recyclerview 吗?
- c - 如何使用信号量让 n 个进程等待?
- git - 将 Bitbucket Repo 更改为项目后,如何更改我的本地 git?
- context-free-grammar - 为单词列表生成 CFG
- firebase - 无法通过遵循官方 Microsoft 示例接收 FCM 后台通知
- python - pip 因 AttributeError 失败:“模块”对象没有属性“_init_cffi_1_0_external_module”
- babeljs - Parcel:如何使“React”全局化,而不是在每个文件中都导入它
- c - unsigned int 变量如何存储负值?