如何监听React类对象实例的变化?

huangapple go评论98阅读模式
英文:

How to listen to changes in instance of a class object in React?

问题

I'm here to provide translations. Here's the translation of your text:

"你好吗?

我试图监听 React 应用中的一个 Class 对象实例的更改。我可以看到类的值发生了变化,但我无法让 useEffect 监听这些变化!

这个类本身非常简单:

  1. class Person {
  2. constructor(name) {
  3. this.name = name;
  4. this.hunger = 0;
  5. }
  6. increaseHunger() {
  7. this.hunger += 10;
  8. }
  9. }
  10. export default Person;

然后我有我的 React 应用组件:

  1. import React from "react";
  2. import Person from "./classes/Person";
  3. export default function App() {
  4. const [person, setPerson] = React.useState(new Person("John"));
  5. React.useEffect(() => {
  6. console.log("Effect");
  7. }, [person.hunger]);
  8. return (
  9. <div>
  10. <div>{person.name}</div>
  11. <div>{person.hunger}</div>
  12. <button onClick={() => person.increaseHunger()}>Click</button>
  13. </div>
  14. );
  15. }

每当我点击按钮时,我可以看到类函数内部的日志,我也可以看到饥饿值发生变化,但 useEffect 函数中的日志从未触发。我认为可能是 React 组件中的 person 对象的引用没有发生变化,但我无法弄清楚为什么!

有没有人知道我可能做错了什么?"

英文:

how are you?

I'm trying to listen to the changes in an instance of a Class object in a React App. I can see that the value of the class changes, but i can't get a useEffect to listen to this changes!

The class itself is pretty simple:

  1. class Person {
  2. constructor(name) {
  3. this.name = name;
  4. this.hunger = 0;
  5. }
  6. increaseHunger() {
  7. this.hunger += 10;
  8. }
  9. }
  10. export default Person;

Then i have my React App component:

  1. import React from &quot;react&quot;;
  2. import Person from &quot;./classes/Person&quot;;
  3. export default function App() {
  4. const [person, setPerson] = React.useState(new Person(&quot;John&quot;));
  5. React.useEffect(() =&gt; {
  6. console.log(&quot;Effect&quot;);
  7. }, [person.hunger]);
  8. return (
  9. &lt;div&gt;
  10. &lt;div&gt;{person.name}&lt;/div&gt;
  11. &lt;div&gt;{person.hunger}&lt;/div&gt;
  12. &lt;button onClick={() =&gt; person.increaseHunger()}&gt;Click&lt;/button&gt;
  13. &lt;/div&gt;
  14. );
  15. }

Whenever i click the button, i can see the log inside the class functions and i can see that the hunger changes, but the log in the useEffect functions never triggers. I think that perhaps the reference of the person object in the React component is not changing, but i can't figure out why!

Does anyone knows what i'm may be doing wrong?

答案1

得分: 2

你需要更新状态,这样你的 person.hunger 才能触发并执行你的 useEffect

  1. const handleChange = () => {
  2. setPerson((prev) => {
  3. return { ...prev, hunger: person.hunger + 10 };
  4. });
  5. };

请查看 LIVE DEMO

英文:

You need to update the state so your person.hunger can be trigger and execute your useEffect .

  1. const handleChange = () =&gt; {
  2. setPerson((prev) =&gt; {
  3. return { ...prev, hunger: person.hunger + 10 };
  4. });
  5. };

Please see LIVE DEMO

huangapple
  • 本文由 发表于 2023年5月7日 11:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定