Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array from useState as prop

In the example here. I have to sliders and I drag them. Because I have two values I store them in useState as an array. Then storing as an array the ComponentsWithArrayAsProp1 doesn't see changes in the state as it is the same array and does not re-render itself.

In the second example, I store values as values.toString(), but this is not a good solution.

What is a good solution for this case?

like image 988
strdr4605 Avatar asked Mar 10 '26 07:03

strdr4605


1 Answers

Then storing as an array the ComponentsWithArrayAsProp1 doesn't see changes in the state as it is the same array and does not re-render itself.

you are right we can solve it by creating new array everytime as follows,

function onChange1(values) {
    setValues1([...values]);
  }

but i think there is something wrong with react-slider .because your approach of setting array ,setValues1(values) works when we click on different points of slider1. but it deosn't work when we drag it. there is something else going wrong here ?

like image 66
Ayyappa Gollu Avatar answered Mar 12 '26 18:03

Ayyappa Gollu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!