React slow with multiple controlled text inputs

后端 未结 8 1154
心在旅途
心在旅途 2021-02-18 13:08

I have a form with multiple text inputs. I have them all set up as controlled inputs. When typing, there is a lag of up to several seconds for the new text to display in the fie

相关标签:
8条回答
  • 2021-02-18 13:53

    I had a similar situation and my solution was to disable React Dev Tools. They were affecting input fields somehow. The problem is it's not enough to refresh a page if you have clicked React Dev Tools tab. They are still affecting your inputs. You have to open new page to stop them. You can also remove them from Chrome completely but I don't recommend doing this 'cause they are useful. :)

    0 讨论(0)
  • 2021-02-18 13:53

    There are many possible reasons for this to happen. I faced the similar issue and filtered the main cause to:

    • Large State, so it takes up sometime
    • React Dev Tool / Use of non minified react
    • Mutating state data

    Whatever may be the reason, I found a quick fix for this. If you just want to store it to the state but not using it for live rendering. Then you can safely replace the 'onChange' to 'onBlur'. This has no dealay and lag. If you know any other case where this will not work, do let me know!

    Change the code as follow:

    <label>Event Name</label>
    <input 
       type="text" 
       placeholder="Title"
       className="form-control"
       name="title"
       value={this.state.event.title}
       onBlur={this.handleChange} />
    
    0 讨论(0)
提交回复
热议问题