admin管理员组文章数量:1403452
On datepicker I have an onChange
event. The problem is when a user starts to manually change date.
if user only changes day, the onChange
function is called and will send req for data.
And if user continue to make changes manually, every time onChange
is executed, which causes performance issue.
How can I avoid this problem?
Sample of Datepicker code:
<Form.Field>
<label style={{ float: 'left' }}>From</label>
<input
ref={i => {
this.reportDateStartedField = i
}}
onChange={this.handleFieldChange.bind(this)}
type="date"
name="reportDateStarted"
value={filters.reportDateStarted}
max={todayDate}
style={{ fontSize: '0.9em' }}
/>
On datepicker I have an onChange
event. The problem is when a user starts to manually change date.
if user only changes day, the onChange
function is called and will send req for data.
And if user continue to make changes manually, every time onChange
is executed, which causes performance issue.
How can I avoid this problem?
Sample of Datepicker code:
<Form.Field>
<label style={{ float: 'left' }}>From</label>
<input
ref={i => {
this.reportDateStartedField = i
}}
onChange={this.handleFieldChange.bind(this)}
type="date"
name="reportDateStarted"
value={filters.reportDateStarted}
max={todayDate}
style={{ fontSize: '0.9em' }}
/>
Share
Improve this question
edited Jan 18, 2019 at 15:17
Martijn Vissers
7821 gold badge6 silver badges30 bronze badges
asked Jan 18, 2019 at 15:07
James DelaneyJames Delaney
1,7862 gold badges21 silver badges38 bronze badges
1
-
1
Generally that's
onChange
purpose. If you don't want to track all the changes by the user you should use anonClick
listener but that means that the user has to press a button or something. – Fotis Papadamis Commented Jan 18, 2019 at 15:12
3 Answers
Reset to default 1I would not call it a performance issue. Since react follows one way data binding, it is up the the developer to decide when to call the render(or defer rendering). Virtual DOM should be in sync.
On the side note you could use 'onBlur' instead of 'onChange' to trigger change only when user has finished typing and focused out.
<Form.Field>
<label style={{ float: 'left' }}>From</label>
<input
ref={i => {
this.reportDateStartedField = i
}}
onBlur={this.handleFieldChange.bind(this)}
type="date"
name="reportDateStarted"
defaultValue={filters.reportDateStarted}
max={todayDate}
style={{ fontSize: '0.9em' }}
/>
In above case the input will be 'uncontrolled' (defaultValue) since react is not aware of the change until user focus out of the input.
Probably the easiest solution would be to not use the onChange
event to fetch data but only use it to control the input and have a button onClick
to do the fetching.
I had a similar issue with react js.
When I change date, app does not made change. It's forzen current date.
I have solved this issue by using onFocus
instead of onBlur
or onClick
since datepicker automatically focuses on text field which does made the change (in my case). Also, use onChange
on top of onFocus
if needed.
<Form.Field>
<label style={{ float: 'left' }}>From</label>
<input
ref={i => {
this.reportDateStartedField = i
}}
onFocus={this.handleFieldChange}
onChange={this.handleFieldChange}
type="date"
name="reportDateStarted"
defaultValue={filters.reportDateStarted}
max={todayDate}
style={{ fontSize: '0.9em' }}
/>
I hope it helps.
本文标签: javascriptDatepicker onChange issue ReactStack Overflow
版权声明:本文标题:javascript - Datepicker onChange issue React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744369673a2602957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论