[英]Why I can't get the right state when I try to console log inside on my onchange handler?
I have problem regarding on getting all the state inside of my HandlerInputChange the data already updated into 10 data however when I console it log inside of my handler it shows the default array value which is 5.我在获取 HandlerInputChange 中的所有状态时遇到问题,数据已经更新为 10 个数据,但是当我控制台它登录到我的处理程序中时,它显示了默认数组值 5。
Question: How to get the all the state data inside of my handler?问题:如何获取处理程序中的所有状态数据?
Goal: To get all the state on my handler目标:让我的处理程序获得所有状态
Logs:日志:
My UseState:我的使用状态:
const [data, setData] = useState(defaultArray)
My Handler:我的处理程序:
useEffect(() => {
console.log("data" , data)
}, [data])
const handleInputChange = (e, i) => {
const { name, value } = e.target;
const list = [...data];
console.log(list);
}
Add Rows 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 disabled selected>Select Customer</option>
<option >LEXI INC / joseph</option>
<option >LEXI INC / james</option>
<option >LEXI INC / charlie</option>
<option >LEXI INC / pam</option>
<option >LEXI INC / regorio</option>
</select>,
bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,val)}>
<option disabled selected>Select Bank</option>
<option >BDO</option>
<option >MAY BANK</option>
<option >EAST WEST BANK</option>
<option >UNION BANK</option>
<option >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 disabled selected>Select Remarks</option>
<option >Test</option>
</select>
}
});
setData([...data, ...newArr]);
}
Button:按钮:
<button type="submit" class="btn btn-danger" onClick={() => AddRows()}>Add Row</button>
Your handleInputChange is not the problem.您的 handleInputChange 不是问题。 Normally, it will be return an array with 10 elements like the variable defaultArray.
通常,它将返回一个包含 10 个元素的数组,例如变量 defaultArray。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.