reactjs – 如何使用重构的toClass HOC将ref添加到功能组件?
发布时间:2020-12-15 20:47:27 所属栏目:百科 来源:网络整理
导读:我正在尝试在React Native中添加一个函数组件的ref,以便在FlatList组件上使用scrollToEnd. 我想使用重构为此,并且正如他们的文档所述,toClass()应该能够处理这个问题.但是,没有给出任何例子. 目前,这是我失败的实施.有人能告诉我我做错了什么吗? 我非常感激
我正在尝试在React Native中添加一个函数组件的ref,以便在FlatList组件上使用scrollToEnd.
我想使用重构为此,并且正如他们的文档所述,toClass()应该能够处理这个问题.但是,没有给出任何例子. 目前,这是我失败的实施.有人能告诉我我做错了什么吗? 我非常感激! import React from 'react'; import PropTypes from 'prop-types'; import { FlatList,View,Text } from 'react-native'; import { graphql } from 'react-apollo'; import { compose,toClass,lifecycle } from 'recompose'; import CommentItem from './CommentItem'; import { commentsQuery } from '../../queries/comments'; const CommentScreen = ({ onRef,currentUser,data: { comments,loading } }) => { if (loading) { return ( <View> <Text>Loading...</Text> </View> ); } return ( <FlatList ref={ref => { this.refs.commentList = ref; }} data={comments} keyExtractor={item => item.id} renderItem={({ item }) => <CommentItem {...item} currentUser={currentUser} />} /> ); }; export default compose( toClass,graphql(commentsQuery),lifecycle({ componentDidMount() { console.log('PROPZZZ',this.commentList); },}),)(CommentScreen); CommentScreen.propTypes = { fetchComments: PropTypes.func.isRequired,updateId: PropTypes.number.isRequired,comments: PropTypes.arrayOf(Object),text: PropTypes.string.isRequired,};
如果需要使用ref,只需使用类组件而不是功能组件.但是,如果由于某些原因需要使用功能组件,则可以使用withHandlers.有关如何使用它,请参阅
this SO question的答案.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |