I have a global variable that will change often. Let's say it is stored in window.something
. In react I need this change to be reflected in the component as well as in its state.
Example code:
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { something: '1'}
}
render() {
return (
<div>
<input value={window.something}
onChange={event => {this.setState({'something': event.target.value})}}
/>
</div>
)
}
}
However the value gets set only for the first time and there is no change as the variable gets updated.