2017-08-14 80 views
4

我的具体目标是使用ScrollView的ScrollTo method,但保持功能组件结构。如何在Recompose中使用withHandlers将函数组件添加到函数组件中,并在ScrollView上调用ScrollTo?

更一般地这需要获取当前组件isn't possible with naked react native

2016年12月重组添加Allows handlers property of withHandlers to be a factory function但我不能完全弄清楚如何正确使用它。

如何在Recompose中使用withHandlers添加refs到功能组件并在ScrollView上调用ScrollTo?

回答

8

你可以尝试这样的事情:

/* ... */ 

const MyView = ({ onRef, children }) => (
    <View> 
     <ScrollView ref={onRef} /* ... */> 
      {children} 
     </ScrollView> 
    </View> 
) 

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) 
+1

这是真棒。谢谢! – GollyJer

相关问题