You can use inline function with a key param in the onClick handler:
import React, { useState } from "react";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ToggleButton from "@material-ui/lab/ToggleButton";
import ToggleButtonGroup from "@material-ui/lab/ToggleButtonGroup";
export default function MultipleToggleSelection() {
const mylist = {
a: "1",
b: "1",
c: "1",
d: "1"
};
const [oneTwoThree, setOneTwoThree] = useState(mylist);
const handleToggle = (key, value) => {
oneTwoThree[key] = value;
setOneTwoThree({...oneTwoThree});
};
return (
<div>
<div>{JSON.stringify(oneTwoThree, null, ' ')}</div>
<List>
{Object.keys(oneTwoThree).map((key, index) => (
<ListItem key={key}>
<ToggleButtonGroup
value={oneTwoThree[index]}
exclusive
onChange={(event, value) => handleToggle(key, value)}
>
<ToggleButton value="1">1</ToggleButton>
<ToggleButton value="2">2</ToggleButton>
<ToggleButton value="3">3</ToggleButton>
</ToggleButtonGroup>
{key}
</ListItem>
))}
</List>
</div>
);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…