Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

onPress button scroll to specific section in functional component

I’m trying to make a code where I want to action on onPress button to assign a screen or section where my scroller should go as I wanted, the below code is perfectly coded using class component, but my entire code Im coding is in functional components and i have no idea how to convert this class component into functional component help indeed.

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',
  },
});

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

let scrollYPos = 0;

export const 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={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',
  },
});

Hope this helps

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading