Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidrenne/28e123e27417f3326aee18363f69bc77 to your computer and use it in GitHub Desktop.
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
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;
@davidrenne
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment