I want make program that table content is changed by user’s select. So I put empty array in useState, like this.
JavaScript
x
2
1
const [statInfo, setStatInfo] = useState([]);
2
And select html code:
JavaScript
1
11
11
1
<select
2
name="statSelect"
3
onChange={(e) => {
4
handleChange(e);
5
setStat();
6
}}>
7
<option value="0to60">무각 60</option>
8
<option value="1to60">1각 60</option>
9
<option value="2to60">2각 60</option>
10
</select>
11
When I select option, data will be changed. But I don’t no how to do it with useState. These are data
JavaScript
1
6
1
const stat = [
2
[11421, 1059, 782, "10%", "50%", "10%", "25%"],
3
[11992, 1112, 821, "15%", "55%", "20%", "30%"],
4
[10401, 1114, 1049, "20%", "30%", "35%", "25%"]
5
];
6
And my React code:
JavaScript
1
14
14
1
async function setStat() {
2
console.log('calling');
3
if (stat === "무각 60") {
4
await setStatInfo(stat[0]);
5
console.log(statInfo);
6
} else if (stat === "1각 60") {
7
await setStatInfo(stat[1]);
8
console.log(statInfo);
9
} else {
10
await setStatInfo(stat[2]);
11
console.log(statInfo);
12
}
13
}
14
I tried using map. But I don’t know how to use map, too.
Is there a function or way to change the entire array each time?
Advertisement
Answer
I guess you can handle this action in your handleChange :
JavaScript
1
8
1
handleChange(e) {
2
stat.forEach(async (att) => {
3
if(att === e.target.value){
4
await setStatInfo(att)
5
}
6
})
7
}
8
If you can’t and you want to use your setStat function I guess you can make something like :
JavaScript
1
8
1
setStat = async (e) => {
2
stat.forEach(async (att) => {
3
if(att === e.target.value){
4
await setStatInfo(att)
5
}
6
})
7
}
8
And call this.setStat(e)
in your onChange