How do you add refs to functional components using withHandlers in Recompose and call ScrollTo on a ScrollView?

后端 未结 3 873
囚心锁ツ
囚心锁ツ 2021-02-13 21:12

My specific goal is to use the ScrollTo method of a ScrollView but maintain functional component structure.

More generally this requires getting ref to the current compo

3条回答
  •  臣服心动
    2021-02-13 22:04

    You can try something like this:

    /* ... */
    
    const MyView = ({ onRef, children }) => (
        
            
                {children}
            
        
    )
    
    export default compose(
        withHandlers(() => {
            let myScroll = null;
    
            return {
                onRef: () => (ref) => (myScroll = ref),
                scrollTo: () => (value) => myScroll.scrollTo(value)
            }
        },
        lifecycle({
            componentDidMount() {
                this.props.scrollTo({ x: 0, y: 100, animated: true })
            }
        })
    )(MyView)
    

提交回复
热议问题