I have a database
[
{
"ID": 3,
"fourth_ip": "5",
"location": "jp",
"third_ip": "20"
},
{
"ID": 4,
"fourth_ip": "123",
"location": "jp",
"third_ip": "21"
},
{
"ID": 5,
"fourth_ip": "123",
"location": "hk",
"third_ip": "20"
},
{
"ID": 6,
"fourth_ip": "200",
"location": "sg",
"third_ip": "11"
},
{
"ID": 7,
"fourth_ip": "6",
"location": "jp",
"third_ip": "20"
},
{
"ID": 8,
"fourth_ip": "200",
"location": "sg",
"third_ip": "12"
}
]
how can i create 3 select with location, third_ip and fourth_ip, they must be related to each other.
like if localtion is jp, third_ip select should be 20,21 and when selected 20 fourth_ip should be 5,6.
Another problem is when I open the page first time, it will be the correct. Location is jp, third_ip is 20 and fourth_ip is 5. But when I select location to hk, third_ip is 20 , but fourth_ip is null.
useEffect(() => {
// setEdgeData(data)
const edgeLocation:never[] = Array.from(new Set(data.map((item: { location: any; }) => item.location)));
setEdgeLocation(edgeLocation);
// setRegion(edgeLocation[0])
if (!region) {
setRegion(edgeLocation[0]);
}
console.log(region)
}, [data]);
useEffect(() => {
setThirdIP("")
const selectedThirdIP:never[] = Array.from(new Set(
data
.filter((item: { location: string; }) => item.location === region)
.map((item: { third_ip: any; }) => item.third_ip)));
console.log(selectedThirdIP)
console.log(selectedThirdIP[0])
setSelectedThirdIP(selectedThirdIP)
if (!thirdIP) {
setThirdIP(selectedThirdIP[0])
}
console.log('thirdip = '+thirdIP)
}, [region]);
console.log('thirdip = '+thirdIP)
useEffect(() => {
const selectedFourthIP:never[] = Array.from(new Set(
data
.filter((item: { location: string, third_ip: string }) => item.location === region && item.third_ip === thirdIP)
.map((item: { fourth_ip: any; }) => item.fourth_ip)));
console.log(selectedFourthIP)
console.log(selectedFourthIP[0])
setSelectedFourthIP(selectedFourthIP)
if (!fourthIP) {
setFourthIP(selectedFourthIP[0])
}
console.log(fourthIP)
}, [region, thirdIP]);
console.log(fourthIP)
<select onChange={handleRegionChange} value={region}>
{edgeLocation.map((location) => (
<option key={location} value={location}>
{location}
</option>
))}
</select>
</p>
<select onChange={handleThirdIPChange} value={thirdIP}>
{selectedThirdIP.map((thirdIP) => (
<option key={thirdIP} value={thirdIP}>
{thirdIP}
</option>
))}
</select>.
<select onChange={handleFourthIPChange} value={fourthIP}>
{selectedFourthIP.map((fourthIP) => (
<option key={fourthIP} value={fourthIP}>
{fourthIP}
</option>
))}
</select>
At fist third_ip and fourth_ip didn’t write in useEffect, but that lead to first time open the page ,if I don’t selected the localtion third_ip and fourth_ip will be null
2
Answers
I just only thought about this
try this.
https://stackblitz.com/edit/react-ts-arjnsj?file=index.tsx
Your code will become slower and slower as long as you use more useeffect.
My suggestion is use two onChange and use a Form to refer to your selects.
I use a lib called Ant Design. It become easier to deal with select. But if you prefer, you can code the options with map, like this:
The third_ip select and fourth_ip select are disabled until they are filled.
If the location select is empty, the others will become empty too.
Please someone let me know if there is any mistake.
Here’s the code: