Obsolete value of "nextValue" property in debounced onChange handler
kettanaito opened this issue · 0 comments
kettanaito commented
What
nextValue
points to the obsolete (step-back) value of the field when it has a debounced logic within onChange
handler.
Why
Pointer issue.
How
export default class FieldUnmounting extends React.Component {
state = {
username: '',
}
handleUsernameChange = debounce(({ nextValue }) => {
this.setState({ username: nextValue })
}, 1000)
render() {
const { username } = this.state
return (
<React.Fragment>
<h1>Debounced change</h1>
<Form>
<Input name="username" onChange={this.handleUsernameChange} />
<p>{username}</p>
</Form>
</React.Fragment>
)
}
}
- Enter "admin" into the field.
- Slowly remove all the letters with backspace, with an interval between key stores lying within the debounce duration.
- See the
this.state.username
value beinga
when the field is empty.
This doesn't affect the fieldProps
, so both internal representation and derived props on the <input/>
have the proper value, but the one exposed as nextValue
points to the a
instead of an empty string.