I have question pertaining to use of Effect Hook, Right now I have module when I click the button it will automatically add 5 new array so each row has onChange this is probably working now. However when I console log the state still no effect but in my view has already effect.
Problem: Why my state still 5 but my rows on my table already 10..
Here is the sample output:
As you can see I have already 10 because I already add new rows but in default 5 rows only. but when I console it logs inside of my onchange handler the state has only 5, there is no reflection happen when I console logs the state.
Based on my research I need to use React Hook Effects, But how to do it in Hooks Effect? I will show you guys my sample code that I created.
Here is the default 5 default state:
const [data, setData] = useState([{
key: "1",
customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,0)} >
<option key="0" disabled selected>Select Customer</option>
<option key="1">LEXI INC / joseph</option>
<option key="1">LEXI INC / james</option>
<option key="1">LEXI INC / charlie</option>
<option key="1">LEXI INC / pam</option>
<option key="1">LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,0)}>
<option key="0" disabled selected>Select Bank</option>
<option key="1">BDO</option>
<option key="1">MAY BANK</option>
<option key="1">EAST WEST BANK</option>
<option key="1">UNION BANK</option>
<option key="1">BPI</option>
</select>,
branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,0)}/>,
checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,0)}/>,
checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,0)}/>,
amount: <input
name="amount"
className="form-control"
placeholder="Amount"
onChange={(e,i) => handleInputChange(e,0)}
/>,
remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,0)}>
<option key="0" disabled selected>Select Remarks</option>
<option key="1">Test</option>
</select>,
},
{
key: "2",
customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,1)}>
<option key="0" disabled selected>Select Customer</option>
<option key="1">LEXI INC / james</option>
<option key="1">LEXI INC / charlie</option>
<option key="1">LEXI INC / pam</option>
<option key="1">LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,1)}>
<option key="0" disabled selected>Select Bank</option>
<option key="1">BDO</option>
<option key="1">MAY BANK</option>
<option key="1">EAST WEST BANK</option>
<option key="1">UNION BANK</option>
<option key="1">BPI</option>
</select>,
branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,1)}/>,
checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,1)}/>,
checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,1)}/>,
amount: <input
name="amount"
className="form-control"
placeholder="Amount"
onChange={(e,i) => handleInputChange(e,1)}
/>,
remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,1)}>
<option key="0" disabled selected>Select Remarks</option>
<option key="1">Test</option>
</select>,
},
{
key: "3",
customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,2)}>
<option key="0" disabled selected>Select Customer</option>
<option key="1">LEXI INC / james</option>
<option key="1">LEXI INC / charlie</option>
<option key="1">LEXI INC / pam</option>
<option key="1">LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,2)}>
<option key="0" disabled selected>Select Bank</option>
<option key="1">BDO</option>
<option key="1">MAY BANK</option>
<option key="1">EAST WEST BANK</option>
<option key="1">UNION BANK</option>
<option key="1">BPI</option>
</select>,
branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,2)}/>,
checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,2)}/>,
checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,2)}/>,
amount: <input
name="amount"
className="form-control"
placeholder="Amount"
onChange={(e,i) => handleInputChange(e,2)}
/>,
remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,2)}>
<option key="0" disabled selected>Select Remarks</option>
<option key="1">Test</option>
</select>,
},
{
key: "4",
customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,3)}>
<option key="0" disabled selected>Select Customer</option>
<option key="1">LEXI INC / james</option>
<option key="1">LEXI INC / charlie</option>
<option key="1">LEXI INC / pam</option>
<option key="1">LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,3)}>
<option key="0" disabled selected>Select Bank</option>
<option key="1">BDO</option>
<option key="1">MAY BANK</option>
<option key="1">EAST WEST BANK</option>
<option key="1">UNION BANK</option>
<option key="1">BPI</option>
</select>,
branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,3)}/>,
checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,3)}/>,
checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,3)}/>,
amount: <input
name="amount"
className="form-control"
placeholder="Amount"
onChange={(e,i) => handleInputChange(e,3)}
/>,
remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,3)}>
<option key="0" disabled selected>Select Remarks</option>
<option key="1">Test</option>
</select>,
},
{
key: "5",
customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,4)}>
<option key="0" disabled selected>Select Customer</option>
<option key="1">LEXI INC / james</option>
<option key="1">LEXI INC / charlie</option>
<option key="1">LEXI INC / pam</option>
<option key="1">LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,4)}>
<option key="0" disabled selected>Select Bank</option>
<option key="1">BDO</option>
<option key="1">MAY BANK</option>
<option key="1">EAST WEST BANK</option>
<option key="1">UNION BANK</option>
<option key="1">BPI</option>
</select>,
branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,4)}/>,
checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,4)}/>,
checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,4)}/>,
amount: <input
name="amount"
className="form-control"
placeholder="Amount"
onChange={(e,i) => handleInputChange(e,4)}
/>,
remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,4)}>
<option key="0" disabled selected>Select Remarks</option>
<option key="1">Test</option>
</select>,
}])
const [list, setList] = useState(data);
Button Where when I click this it will add new 5 rows:
<button type="submit" class="btn btn-danger" onClick={() => addRows()}>Add Row</button>
AddRows Function:
const addRows = () => {
//loop here the keys listed
let initialKey = 0;
const x = data.slice(-1);
initialKey = parseInt(x[0]['key']);
let newArr=[...Array(5)].map((_,i)=>{
let val=i + 1 +initialKey;
return {
key: ""+val+"",
customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,val)}>
<option key="0" disabled selected>Select Customer</option>
<option key="6">LEXI INC / joseph</option>
<option key="6">LEXI INC / james</option>
<option key="6">LEXI INC / charlie</option>
<option key="6">LEXI INC / pam</option>
<option key="6">LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,val)}>
<option key="0" disabled selected>Select Bank</option>
<option key="6">BDO</option>
<option key="6">MAY BANK</option>
<option key="6">EAST WEST BANK</option>
<option key="6">UNION BANK</option>
<option key="6">BPI</option>
</select>,
branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,val)}/>,
checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,val)}/>,
checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,val)}/>,
amount: <input
name="amount"
className="form-control"
placeholder="Amount"
onChange={(e,i) => handleInputChange(e,val)}
/>,
remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,val)}>
<option key="0" disabled selected>Select Remarks</option>
<option key="6">Test</option>
</select>
}
});
setData([...data,...newArr])
}
Here is function of OnChange
const handleInputChange = (e, i) => {
console.log(i);
const { name, value } = e.target;
const list = [...data];
console.log(list);
const x = list[i];
// const x = list[i][name] = value;
console.log(x)
}
Output Onchange: Based on the logs the state is 5 only.

