I'm having a bit of trouble with controlling the amount of re rendering done by React. Say I've got this user input right here:
handleFirstNameChange: function(e) {
this.setState({ firstName: e.target.value, userTyping: true });
},
render: function(){
<RB.Input value={this.state.firstName} onChange={this.handleFirstNameChange} name=""
type="text" label="First name" placeholder="First name.."/>
}
And this custom dropdown component:
<SearchDropdown title="Role" options={this.props.currentUser.roles}
selectedOptions={this.props.currentUser.selectedRoles} />
Every single time the user inputs a letter, the dropdown will re render every single option, even though (as it seems) the components have nothing common to each other. I've been trying to manage this by including a "userIsTyping" boolean as a prop to the dropdown component and checking it in ComponentShouldUpdate but it's becoming more of a problem now that I've got nested components. I'm still in my early stages of using React so I'm pretty sure there's something obvious here that I'm missing.
With React,
onChange
fires every time a key is pressed. In your code, this will in turn cause a call tosetState
via thehandleFirstNameChange
handler, and that will cause a re-render.Two options:
onChange
, useonBlur
and then your handler will only fire when the field loses focus.You can read more about the behaviour of
onChange
in React in the documentation:https://facebook.github.io/react/docs/forms.html#interactive-props