如何在像angular这样的reactjs中获取或更新变量中的值?

How to get or update the value in variables in reactjs like angular?

是否有任何选项可以执行以下操作?

将class切换到reactjs中的特定元素

这是函数

variable: any

onclick() {
  this.variable = 'new value';
}

<img src={ham} className="cursor-pointer" width="40" alt="" onClick={this.handleClick} />
<div className={this.variable + ' cursor-pointer'}>
  Content
</div>

如果您想切换传递给元素的 className,则存储布尔状态并有条件地添加类名。在点击处理程序中切换状态值。

type MyState = {
  variable: boolean;
};

class Header extends Component<any, MyState> {

  state: MyState = {
    variable: false
  }

  handleClick = () => {
    this.setState(prevState => ({ variable: !prevState.variable }));
  }

  ...

  <img
    src={ham}
    className="cursor-pointer"
    width="40"
    alt=""
    onClick={this.handleClick}
  />
  <div className={this.state.variable ? 'cursor-pointer' : ''}>
    Content
  </div>