Created
June 6, 2018 14:41
-
-
Save davidrenne/28e123e27417f3326aee18363f69bc77 to your computer and use it in GitHub Desktop.
This is an example abstraction for TextField MUI slowness to convert your onChange callers to set parent state onBlur instead and keep state local to render the individual textbox component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MUIField from 'material-ui/TextField'; | |
import React, {Component} from 'react'; | |
class TextField extends Component { | |
constructor(props, context) { | |
super(props, context); | |
this.state = { | |
value: props.value || props.defaultValue | |
}; | |
} | |
render() { | |
let props = {}; | |
Object.keys(this.props).forEach((k) => { | |
if (k !== "onChange" && k !== "value" && k !== "defaultValue") { | |
props[k] = this.props[k]; | |
} | |
}); | |
return <MUIField | |
{...props} | |
value={this.state.value} | |
onChange={(e) => { | |
this.setState({value: e.target.value}); | |
}} | |
onBlur={() => { | |
this.props.onChange({ | |
target: { | |
value: this.state.value | |
} | |
}); | |
}}/> | |
} | |
} | |
export default TextField; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note I am mimicking the event object on the response so you dont have to update your callers who may have had an onChange event will merely get the new value onBlur to prevent re-renders every keystroke.