0

I am using this library called react-native-multi-slider to implement a multi slider for a mobile application using React Native.

I was going through the example in the repository and found something odd that works somehow.

There are functions without any variable declaration in the code.

const [multiSliderValue, setMultiSliderValue] = React.useState([3, 7]);
const [sliderOneChanging, setSliderOneChanging] = React.useState(false);

sliderOneValuesChangeFinish = () => setSliderOneChanging(false);

multiSliderValuesChange = values => setMultiSliderValue(values);

I initially thought this was a mistake but I used the code in my application and it works without any runtime errors and these "functions" work as intended.

For example, multiSliderValuesChange sets the values that are passed into it to setMultiSliderValues() and when I log the values of multiSliderValue it does match the values on the slider.

multiSliderValuesChange is passed in as a prop to onValuesChange which is a callback to update the values when they change, as per the documentation.

<MultiSlider
          values={[multiSliderValue[0], multiSliderValue[1]]}
          sliderLength={250}
          onValuesChange={multiSliderValuesChange}
          min={0}
          max={10}
          step={1}
          allowOverlap
          snapped
          customLabel={CustomLabel}
        />

I went into the type definitions file of the MultiSlider component and onValuesChange takes as input an array of numbers and returns nothing:

onValuesChange?: (values: number[]) => void;

But I still do not understand how it works without any variable declarations such as var, let or const.

yudhiesh
  • 6,383
  • 3
  • 16
  • 49
  • 1
    It works, it's just really sloppy code. They should fix it. – CertainPerformance Jan 07 '21 at 04:21
  • Without `let`, `const` or `var` the variable will be defined on the closest scope where it already exists or the global scope (`window` in a browser). This can lead to issues if multiple instances of the code are used at the same time (overwriting the variable) or if two separate sections of code use the same variable name omitting `let`, `const` and `var`. – Jacob Smit Jan 07 '21 at 04:26

0 Answers0