如何解决React Rerenderign问题

发布于 2025-02-11 23:14:57 字数 800 浏览 1 评论 0原文

具有总和函数和总和作为使用效应的依赖性。每当输入更改Console.log(“ sum as as”)打印时。 Console.log(“ sum is is”)即使输入更改也没有依赖性,也不会打印。这是因为当输入更改时,它将分配给组件和总和函数地址,这是为什么它触发使用效应或可能不同的原因。

import "./styles.css";
import { useEffect, useState } from "react";
export default function App() {
  const [num1] = useState(4);
  const [num2] = useState(5);
  const [input, setInput] = useState();
  const sum = () => num1 + num2;
  useEffect(() => {
    console.log("sum is");
  }, [num]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <input onChange={(e) => setInput(e.target.value)} />
    </div>
  );
}

have sum function and sum as dependency of useEffect. whenever the input change console.log("sum is") printed. console.log("sum is") is not printed when there is no dependency in useEffect even if input change. it is because of when input change it rerender the component and sum function address will be assign to different thats why it trigger the useEffect or may be different.

import "./styles.css";
import { useEffect, useState } from "react";
export default function App() {
  const [num1] = useState(4);
  const [num2] = useState(5);
  const [input, setInput] = useState();
  const sum = () => num1 + num2;
  useEffect(() => {
    console.log("sum is");
  }, [num]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <input onChange={(e) => setInput(e.target.value)} />
    </div>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫以 2025-02-18 23:14:57

使用效应是用于制作某种共同调整的React钩子

// 1. No dependency passed:
useEffect(() => {
  //Runs on every render
});
// 2. An empty array:
useEffect(() => {
  //Runs only on the first render
}, []);
// 3. Props or state values:
useEffect(() => {
  //Runs on the first render
  //And any time any dependency value changes
}, [prop, state]);

useEffect is a React hook used to make some kind of co-routines, it is triggered when it dependencies are updated or when the component is rendered

// 1. No dependency passed:
useEffect(() => {
  //Runs on every render
});
// 2. An empty array:
useEffect(() => {
  //Runs only on the first render
}, []);
// 3. Props or state values:
useEffect(() => {
  //Runs on the first render
  //And any time any dependency value changes
}, [prop, state]);
听你说爱我 2025-02-18 23:14:57

原因console.log(“ sum is”)在每个渲染上执行是因为sum

const f1 = () => 1 + 2;
const f2 = () => 1 + 2;
const isEqaul = f1 === f2;
console.log(isEqual); // output is false

在您的情况下const sum =()=&gt; NUM1 + NUM2在每个渲染上都有不同的值重新分配,并且useffect识别sum的值已更改。

要解决问题,您可以:

  1. 定义sum为值:cosnt sum = num1 + num1 + num2
  2. 使用usecallback用于记忆的挂钩该函数(换句话说,该函数将存储在内存中,因此在每个渲染中都相等)
const sumCallback = useCallback(
  () => num1 + num2,
  [num1, num2],
);

,只要NUM1和NUM2未分配新值,sumcallback将具有相同每次价值

The reason console.log("sum is") is executed on every render is because sum is a function and not a value, and when a function is compared to another function, even if its defined exactly the same way, the equality operator will return false

For example:

const f1 = () => 1 + 2;
const f2 = () => 1 + 2;
const isEqaul = f1 === f2;
console.log(isEqual); // output is false

In your case const sum = () => num1 + num2 is reassigned with a different value on each render, and useEffect identifies that the value of sum had changed.

To solve the problem, you can:

  1. Define sum as a value: cosnt sum = num1 + num2
  2. Use the useCallback hook which is used to memoize the function (in other words, the function will be stored in memory and therefore be equal in each render)
const sumCallback = useCallback(
  () => num1 + num2,
  [num1, num2],
);

Now, as long as num1 and num2 aren't assigned with new values, sumCallback will have the same value each time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文