javascript - Datepicker onChange issue React - Stack Overflow

On datepicker I have an onChange event. The problem is when a user starts to manually change date.if us

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 an onClick listener but that means that the user has to press a button or something. – Fotis Papadamis Commented Jan 18, 2019 at 15:12
Add a ment  | 

3 Answers 3

Reset to default 1

I 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744369673a4570883.html

相关推荐

  • javascript - Datepicker onChange issue React - Stack Overflow

    On datepicker I have an onChange event. The problem is when a user starts to manually change date.if us

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信