Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native slider is having lagging issue

React Native slider onValueChange calling setState makes slider lag.

I also tried debounce function but it not solve my problem as I have to show the change value to the screen. hence without using debounce from lodash the slider is laggy and when use debounce the slider movement a bit smooth than previous method but the change in value(state value which I have to show on screen) is not instantaneously changing, change in value reflecting on screen is laggy means it taking time to show on screen.

import React from "react";
import Slider from "react-native-slider";
import { StyleSheet, View, Text } from "react-native";

export default class SliderExample extends React.Component {

  state = {
    value: 0.2
  };

  render() {
    return (
      <View style={styles.container}>
        <Slider
          value={this.state.value}
          onValueChange={value => {
              this.setState({ value })
                // this.props.changeState(this.state.value)
                console.log(this.state.value)
            }}
          maximumValue={100}
          step={1}
        />
        <Text>
          Value: {this.state.value}
        </Text>
      </View>
    );
  }
}

Also one thing is that when I only implement this above slider then there is no problem but when I use it in full code where there are many states then its creates problem.

like image 515
Rajat Verma Avatar asked Jun 21 '19 17:06

Rajat Verma


1 Answers

Try this solution, you reduce the number of updates :

onValueChange={value => {
  clearTimeout(this.sliderTimeoutId)
  this.sliderTimeoutId = setTimeout(() => {
    this.setState({value})
  }, 100)
}}
like image 129
L.G Avatar answered Sep 30 '22 19:09

L.G