I'm using react and would like an input text that displays a value from an in-memory data store, then updates the store when it's value changes, which triggers a re-render. The issue I'm seeing is that my input text loses focus on the rerender. I've tried setting the key attribute and overriding shouldComponentUpdate and returning false to prevent react from rerendering the component, but to no avail. It seems that the latter may not be a good long-term solution anyway.
From the docs:
Note that in the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component.
Code:
export class TextInput extends React.Component {
constructor(props) {
super();
this.storePath = props.value;
const value = dataStore.get(this.storePath) || '';
this.state = { value };
}
shouldComponentUpdate() { return false; }
update(event) {
dataStore.update(this.storePath, event.target.value);
}
render() {
return <input type="text" value={this.state.value} onChange={e => this.update(e)}/>;
}
}
Edit
I am using react router.
const App = props => (
<Router history={browserHistory}>
<div>
<Switch>{
Array.from(this.routeMap.keys()).map(path => {
const viewFn = this.routeMap.get(path);
return <Route key={path} path={path} render={viewFn}></Route>;
})
}</Switch>
</div>
</Router>
);
There is a top-level component that is pulled in through the route and that is using my TextInput component.