简体   繁体   English

为什么当我将@action 装饰器添加到反应事件处理程序 function 时组件不重新渲染

[英]Why does the component is not re-rendering when i add @action decorator to an react event handler function

Problem:问题:

The below react component is not re-rendering when I add @action decorator to the class member fn.当我将@action 装饰器添加到 class 成员 fn 时,以下反应组件不会重新渲染。

import { observable, action } from "mobx";
import { observer } from "mobx-react";
import * as React from "react";
import { render } from "react-dom";

@observer
class PriceCounter extends React.Component {
  @observable price = 0;

  @action
  setPrice = () => {
    this.price = this.price + 1;
  };

  @action
  currentPrice = () => {
    return this.price;
  }

  render() {
    return (
      <div>
        <span className="text-3xl">Total: {this.currentPrice()}</span>
        <button onClick={this.setPrice}>Change details</button>
      </div>
    );
  }
}

render(<PriceCounter />, document.getElementById("root"));

  • The PriceCounter component re-renders as expected when @action is removed from currentPrice fn.当从 currentPrice fn 中删除 @action 时,PriceCounter 组件会按预期重新呈现。

Why?为什么? Explanation needed.需要解释。

Either you can use this.price to show the updated value of price.您可以使用 this.price 来显示价格的更新值。 Or you can use computed decorator to do this.或者您可以使用计算装饰器来执行此操作。

@observer
class PriceCounter extends React.Component {
  @observable price = 0;

  @action
  setPrice = () => {
    this.price = this.price + 1;
  };

  @computed
  get currentPrice() {
    return this.price;
  }

  render() {
    return (
      <div>
        <span className="text-3xl">Total: {this.currentPrice}</span>
        <button onClick={this.setPrice}>Change details</button>
      </div>
    );
  }
}

or或者

@observer
class PriceCounter extends React.Component {
  @observable price = 0;

  @action
  setPrice = () => {
    this.price = this.price + 1;
  };

  @action
  currentPrice = () => {
    return this.price;
  };

  render() {
    return (
      <div>
        <span className="text-3xl">Total: {this.price}</span>
        <button onClick={this.setPrice}>Change details</button>
      </div>
    );
  }
}

This is a wrong usage of action.这是对动作的错误用法。

@action is expected to be wrapped around a function in which observables are changed. @action预计将包裹在 function 周围,其中可观察到的内容已更改。 Here, you are not changing any observables in currentPrice function.在这里,您没有更改currentPrice function 中的任何可观察值。

Since render is a tracked function in mobx-react, if you are not wrapping currentPrice function with action & if you call this.currentPrice() mobx tracks observables used in that function.由于渲染是 mobx-react 中跟踪的 function,如果你没有用 action 包裹currentPrice function 并且如果你调用this.currentPrice() mobx 跟踪在 ZC38541AB5074C178 中使用的 observables

If you are wrapping functions used in a tracked function with action mobx doesn't know if you intend to track observables or do you want to run tracked functions after the function is executed.如果您使用动作包装跟踪 function 中使用的函数,则 mobx 不知道您是否打算跟踪 observables 或者您想在 function 执行后运行跟踪函数。

Actions should only, and always, be used on functions that modify state.操作应仅且始终用于修改 state 的函数。 Have a look at When to use actions查看何时使用操作

In the example you mentioned, currentPrice method is not modifying the state.在您提到的示例中, currentPrice方法没有修改 state。 So you need not use action decorator to that.所以你不需要使用动作装饰器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的反应组件不断重新渲染? - Why does my react component keep re-rendering? 为什么我的组件没有在响应中重新渲染 - why my component is not re-rendering in react 重新渲染React组件 - re-rendering React Component 重新渲染组件React - re-rendering a component React 当路由发生变化时,我可以避免重新渲染父路由处理程序组件吗? - Can I avoid re-rendering parent route handler component when the route changes? 为什么 state 更改后组件没有重新渲染。 在 react-native function 组件中 - why Component is not re-rendering after state change. in a react--native function Component 如何避免在React Component中使用事件侦听器进行无限重新渲染? - How to avoid infinite re-rendering with event listener in React Component? 为什么在更改发送给子组件(react js)的道具的值时会进行组件重新渲染? - Why is a Component Re-Rendering done when changing the value of the props sent to child Component (react js)? 在 React 功能组件中重新渲染 - 为什么会出现这种行为? - Re-rendering in React functional component - why this behavior? React w/hooks:防止使用函数作为道具重新渲染组件 - React w/hooks: prevent re-rendering component with a function as prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM