react-native - React Native Animated.ScrollView 不允许我以编程方式滚动到某个位置

标签 react-native scrollview

在升级到最新版本的 React-Native 和 Expo 之前,这个代码块是有效的。它正在处理的版本是"expo": "^32.0.0" .

我以前能够以编程方式移动 Animated.ScrollView 的 child ,但现在我无法再这样做了。这是我正在测试的测试代码块。

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Animated,
  TouchableOpacity,
  SafeAreaView,
  ScrollView
} from 'react-native';

export default class App extends React.Component {
  constructor(props) {
    super(props);
  }
  handleMove = () => {
    this.scroller.getNode().scrollTo({
      x: 200,
      y: 0,
      animated: false
    });
  };
  render() {
    return (
      <SafeAreaView style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Animated.ScrollView
          horizontal
          ref={scroller => {
            this.scroller = scroller;
          }}
        >
          <View
            style={{
              height: 100,
              width: 100,
              backgroundColor: 'red',
              margin: 5
            }}
          />
        </Animated.ScrollView>
        <TouchableOpacity onPress={this.handleMove}>
          <Text>Move</Text>
        </TouchableOpacity>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {}
});

升级后代码块不再适用于最新版本。我正在测试的当前版本是:
"dependencies": {
    "expo": "^34.0.1",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-web": "^0.11.4"
  },

这样做的正确方法是什么?

我添加了一份零食来帮助说明我的问题。 '
https://snack.expo.io/@louis345/brave-banana

最佳答案

我能够通过使用 1 秒的简单 setTimeout 来完成这项工作。

这是我的代码现在的样子:

setTimeout(() => {
  this.scrollView.scrollTo({
    x: DEVICE_WIDTH * current_index,
    y: 0,
    animated: false
  });
}, 1)

与 Micheal 上面的建议类似,这很可能是由于 React 中已弃用的 componentWillMount 未在 ScrollView 中正确更新而导致的安装问题。

关于react-native - React Native Animated.ScrollView 不允许我以编程方式滚动到某个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57385037/

相关文章:

android - 如何在android中添加多个按钮到scrollview

android - 在启动应用程序时会在android wix react-native-navigation抽屉中崩溃,但在iOS上工作正常

android - ScrollView如何设置多个 View ?

android - 如何更改 scrollView 中的 View 位置,以将其固定在屏幕顶部

java - RelativeLayout : works fine on a 1. 6+API 中的 Horizo​​ntalScrollView,但不是 1.5 API

java - ScrollView 中的静态图像

javascript - React Native 的堆栈导航器和选项卡导航器出现问题

django - 在 native react 中获取 csrf token

javascript - 将字符串拆分为各种文本元素

react-native - react 原生 : 'React' refers to a umd global but the current file is a module consider adding an import instead