javascript - onPress 按钮滚动到功能组件中的特定部分

标签 javascript css react-native react-functional-component

我正在尝试编写一个代码,我想在 onPress 按钮上执行操作,以分配滚动条应按照我的意愿移动的屏幕或部分,下面的代码是使用 完美编码的>类组件,但我的整个代码都是在函数组件中编码的,我不知道如何将这个类组件转换为函数组件确实有帮助。

Apps.js

import React, { Component } from 'react';
import {
  Dimensions,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  Button,
  TouchableOpacity,
  View
} from 'react-native';

let scrollYPos = 0;

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      screenHeight: Dimensions.get('window').height,
      screenWidth: Dimensions.get('window').width,
    };
  }

  scrollToB = () => {
    scrollYPos = this.state.screenHeight * 1;
    this.scroller.scrollTo({ x: 0, y: scrollYPos });
    console.log("test", scrollYPos)
  };
  scrollToC = () => {
    scrollYPos = this.state.screenHeight * 2;
    this.scroller.scrollTo({ x: 0, y: scrollYPos });
  };
  scrollToTop = () => {
    this.scroller.scrollTo({ x: 0, y: 0 });
  };

  render() {
    return (
      <ScrollView style={styles.container} ref={(scroller) => { this.scroller = scroller }}>
        <View style={[styles.screen, styles.screenA]}>
          <Button
            onPress={this.scrollToB}
            title="sctoll to B"
          />
          <Button
            title="sctoll to C"
            onPress={this.scrollToC}
          />
          <Button
            title="sctoll to Top"
            onPress={this.scrollToTop}
          />
          <Text style={styles.letter}>A</Text>
          <View style={styles.scrollButton}>
            <Text style={styles.scrollButtonText}>Scroll to B</Text>
          </View>
        </View>
        <View style={[styles.screen, styles.screenB]}>
          <Text style={styles.letter}>B</Text>
          <View style={styles.scrollButton}>
            <Text style={styles.scrollButtonText}>Scroll to C</Text>
          </View>
        </View>
        <View style={[styles.screen, styles.screenC]}>
          <Text style={styles.letter}>C</Text>
          <View style={styles.scrollButton}>
            <Text style={styles.scrollButtonText}>Scroll to Top</Text>
          </View>
        </View>
      </ScrollView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  screen: {
    backgroundColor: 'yellow',
    flexDirection: 'column',
    height: Dimensions.get('window').height,
    justifyContent: 'center'
  },
  screenA: {
    backgroundColor: '#F7CAC9',
  },
  screenB: {
    backgroundColor: '#92A8D1',
  },
  screenC: {
    backgroundColor: '#88B04B',
  },
});

最佳答案

import React, { Component, useState } from 'react';
import {
  Dimensions,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  Button,
  TouchableOpacity,
  View
} from 'react-native';

let scrollYPos = 0;

export default App = () => {
    const [screenHeight, setScreenHeight] = useState(Dimensions.get('window').height)
    const [screenWidth, setScreenWidth] = useState(Dimensions.get('window').width)

  const scrollToB = () => {
    scrollYPos = screenHeight * 1;
    this.scroller.scrollTo({ x: 0, y: scrollYPos });
    console.log("test", scrollYPos)
  };
  const scrollToC = () => {
    scrollYPos = screenHeight * 2;
    this.scroller.scrollTo({ x: 0, y: scrollYPos });
  };
  const scrollToTop = () => {
    this.scroller.scrollTo({ x: 0, y: 0 });
  };

    return (
      <ScrollView style={styles.container} ref={(scroller) => { this.scroller = scroller }}>
        <View style={[styles.screen, styles.screenA]}>
          <Button
            onPress={scrollToB}
            title="sctoll to B"
          />
          <Button
            title="sctoll to C"
            onPress={scrollToC}
          />
          <Button
            title="sctoll to Top"
            onPress={scrollToTop}
          />
          <Text style={styles.letter}>A</Text>
          <View style={styles.scrollButton}>
            <Text style={styles.scrollButtonText}>Scroll to B</Text>
          </View>
        </View>
        <View style={[styles.screen, styles.screenB]}>
          <Text style={styles.letter}>B</Text>
          <View style={styles.scrollButton}>
            <Text style={styles.scrollButtonText}>Scroll to C</Text>
          </View>
        </View>
        <View style={[styles.screen, styles.screenC]}>
          <Text style={styles.letter}>C</Text>
          <View style={styles.scrollButton}>
            <Text style={styles.scrollButtonText}>Scroll to Top</Text>
          </View>
        </View>
      </ScrollView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  screen: {
    backgroundColor: 'yellow',
    flexDirection: 'column',
    height: Dimensions.get('window').height,
    justifyContent: 'center'
  },
  screenA: {
    backgroundColor: '#F7CAC9',
  },
  screenB: {
    backgroundColor: '#92A8D1',
  },
  screenC: {
    backgroundColor: '#88B04B',
  },
});

希望这有帮助

关于javascript - onPress 按钮滚动到功能组件中的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74277360/

相关文章:

javascript - 如何关闭带有链接或按钮等的html页面?

javascript - 在 JavaScript 中解析文本

javascript - 如何从另一个组件更改组件状态?

react-native - 失败的 Prop 类型无效的 Prop 'value'

react-native - 将颜色变量导入我的样式

javascript - JQuery革命 slider 链接到特定幻灯片

javascript - 覆盖固定在 div 的顶部并保持位置页面滚动

html - 修复 css,使其相对于它的子项(/responsive)

javascript - 如何禁用在 iframe 元素内拖动?

Jquery ui 选项卡添加 css 类