The React project uses antD Input to implement an anti-shock function, but an error occursSyntheticEvent...
.e.target
fornull
.
Cause of the matter
At the very beginningantd
theForm
Used in the component documentationInput
theonChange(value)
Yes, I received it directlyvalue
Later foundInput
Document to seeonChange(e)
It’s actually receivingevent
.
Replace it with the correct oneevent
I’m using the method directly in the outer layerlodash
thedebounce
There is no way to encapsulate the buffering method in the method itself, you can see the code:
const onDiscountChange = debounce((e) = > {
e.persist(); // Even if this is set, it does not work.
console.log(e.target.value,9);
/ /... dosomething
}, 500)
return (
<Form.Item
label="Discount code"
>
<Input
value={discountValidate.value}
placeholder='Please enter the discount code'
onChange={onDiscountChange}
/>
</Form.Item>
Copy the code
Settlement process
Now I thought it was Debounce and tried to get rid of it. Sure enough, everything was fine.
But I want to shake ah, to save resources, improve the experience of the front end engineer. Oh yeah! So the question is, do you hand write one internally? Think about it… trouble
Then start thinking, two methods come to mind
- The onChange event directly passes out the value, which solves the problem of nested methods that cannot be passed
<Input
value={discountValidate.value}
placeholder='Please enter the discount code'
onChange={(e) = > onDiscountChange(e.target.value)}
/>
Copy the code
- The onDiscountChange method does not shake, the internal call interface method encapsulates a separate method
debounceFoo(e.target.value)
, this method to shake it off.
conclusion
Interfaces are required to use debounce packagingevent
The correct event object cannot be retrieved from the function