As given in below code, I store the state, and also I am getting values in the “value” variable after using onChange={(...)=>setValue1(...)}, value.target.ariaValueText
.
My motive is I want to associate it with {key}
mentioned in <Form.Lable>
, so that I can store updated value along with the keys.
JavaScript
x
37
37
1
const [value, setValue1] = React.useState(1);
2
const handleChange=(changeEvent)=>{
3
setValue1(prevState => {prevState, [key]:changeEvent.target.value});
4
//console.log(key);
5
console.log(prevState.key);
6
};
7
8
return <div className={classes.root}>
9
<Form.Label> {key} </Form.Label>
10
<Slider
11
12
defaultValue={dict[key]}
13
aria-labelledby="discrete-slider-custom"
14
getAriaValueText={valuetext}
15
valueLabelDisplay="auto"
16
//getAriaValueText={valuetext}
17
//value={dict[key]}
18
step={0.01}
19
min={min1}
20
max={max1}
21
22
//onChange={} // for example updating a state value
23
//onChangeCommitted={} // for example fetching new data
24
25
//marks={marks_arr[i]}
26
27
//onChange={handleChange}
28
onChange={(changeEvent) =>
29
setValue1(changeEvent.target.value)
30
}
31
32
/>
33
</div>
34
})
35
}
36
</Form.Group>
37
Advertisement
Answer
You can update your type to be an object type to store key-value pairs. for eg:
JavaScript
1
8
1
const [value, setValue1] = React.useState({});
2
3
4
5
6
setValue1({[key]: changeEvent.target.value});
7
// ---------^ this will create/update attribute with value of "key" in your state
8
If you have multiple sliders you’d have to do something like
JavaScript
1
2
1
setValue1(prevState => {prevState, [key]: changeEvent.target.value});
2
to not lose other key-value pairs