I couldn't make the setMajor and setMinor work inside the function.
What happen is, it just overwrite some of the data inside major and keeps on adding data inside minor
I use this code to clear all the value inside the major but it's kinda messy and it looks like I'm not using the useState function properly.
major.forEach((tk) => {
tk['material'] = ''
tk['required'] = 0
tk['actual'] = 0
tk['difference'] = 0
tk['crit'] = 0
tk['percent'] = 0
})
The handleSelectInput is a select tag that whenever I change the value it would send a request to the server to fetch the correct value for the major and minor variables
const majorStates = [
{
tank: 1,
material: '',
required: 0,
actual: 0,
difference: 0,
crit: 0,
percent: 0,
},
{
tank: 2,
material: '',
required: 0,
actual: 0,
difference: 0,
crit: 0,
percent: 0,
},...10],
[major, setMajor] = useState(tankStates),
[minor, setMinor] = useState([]),
const handleSelectInput= async (e) => {
const { name, value } = e.target
setMajor(majorStates) // not working
setMinor([]) // not working
await axios
.put(`${PATH}/main-start`, { product: value })
.then((res) => {
for (let i = 0; i < res.data.length; i++) {
//See if data has a type major or minor
//ParseFloat for decimal that's been returned as str
if (res.data[i].type === 'MAJOR') {
major[i].material = res.data[i].material
major[i].required = parseFloat(res.data[i].required_wt)
setMajor([...major])
} else {
res.data[i].required_wt = parseFloat(res.data[i].required_wt)
setMinor([...minor, res.data[i]])
}
}
})
.catch((err) => {
console.log(err)
})
}
I have a majorStates because it is required that the 10 value is shown even if the fetch data is less than 10.
Sorry if it's a noob question