React中引用的正确原型是什么?

reactjs React.js

猿Gil

2020-03-17

我在我的redux存储中存储了一个引用,并使用mapStateToProps公开了需要访问它的组件的引用。

存储的ref如下所示:

ref={node => this.myRefToBePutInReduxGlobalStore = node}

此引用的正确propType是什么?

第1873篇《React中引用的正确原型是什么?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
StafanHarry 2020.03.17

与@Pandaiolo的帖子非常相似,

PropTypes.elementType 现在已添加

forwardedRef: PropTypes.oneOfType([
  PropTypes.func,
  PropTypes.shape({ current: PropTypes.elementType })
]),

如果使用PropTypes> = 15.7.0 PropTypes.elementType已于2019年2月10日添加到此PR中

村村AL 2020.03.17

我只检查首选方式,由于PropType.object不是很有价值,所以最终使用了这种方式:

PropTypes.shape({ current: PropTypes.instanceOf(Element) })

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android