I want to change the style of a component's element on some event, like a click. But when i do, i get the error that my style object is read-only when its not defined as such and thus immutable. Even defining the style object outside the class creates the same error. I created a simple example to illustrate my problem below.
Error:
Uncaught TypeError: Cannot assign to read only property 'color' of object '#<Object>'
profile.tsx
import * as React from 'react';
export default class Profile extends React.Component {
public styler = {
color:"white",
}
render() {
return (<button style={this.styler} onClick={this.HandleMenuClick.bind(this)}>Click me!</button>);
}
HandleMenuClick() {
this.styler.color = 'gray'; // error on executing this line
}
}
Error created upon clicking the button.
Same error is created when moving the style outside of the class:
var styler = {
color:"white",
}