我的具体目标是使用ScrollView的ScrollTo method,但保持功能组件 struct .

更一般地说,这需要获得isn't possible with naked react native的当前组件的ref.

2016年12月,recompose增加了Allows handlers property of withHandlers to be a factory function,但我不太明白如何正确使用它.

如何在ScrollView上使用重组和调用ScrollTo中的WithHandler向功能组件添加引用?

推荐答案

你可以试试这样:

/* ... */

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)

React-native相关问答推荐

使用下拉菜单时react 本机react-native-dropdown-select-list,它会将下面的项目向下移动.如何阻止这种情况发生

将值从一个 js 文件传递​​到 react native 中的另一个 js 文件:react native

React Native Navigation const { navigate } = this.props.navigation;

React Native 上传到 S3 存储空间

用hooks钩子react context上下文防止重新渲染

如何使用 React Navigation 使 TabNavigator 按钮推送模态屏幕

textAlignVertical不是有效的样式属性

没有找到 `React-fishhook` 的 podspec

使用 React Navigation 制作离开屏幕的动画

有react-native复选框的例子吗?

无法判断模块react-native-maps:找不到名称为 default的配置

如何修复 npm start cannot determine native SDK version错误?

使用来自另一个页面的 fetch 调用函数返回结果值

在 React Native 中使用小数

在 React 中调用 getDerivedStateFromProps 中的方法

使用 React-Native Navigation 传递数据

如何在 React Native 的 MapView 中设置标记

如何在 react-native 中将文本复制到剪贴板?

如何使用 Jest 测试导入自定义原生模块的 React Native 组件?

React Native:无法解析模块 fs