简体   繁体   English

为什么我收到此错误:React Hook useEffect 内部的变量在每次渲染后都会丢失?

[英]Why i am getting this error: variable from inside React Hook useEffect will be lost after each render?

i am getting this error warning Assignments to the 'height' variable from inside React Hook useEffect will be lost after each render.我收到此错误警告分配给 React Hook useEffect 内部的“高度”变量将在每次渲染后丢失。 To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property.要随着时间的推移保留该值,请将其存储在 useRef Hook 中并将可变值保留在“.current”属性中。 Otherwise, you can move this variable directly inside useEffect react-hooks/exhaustive-deps否则,您可以直接在 useEffect react-hooks/exhaustive-deps 中移动此变量

this is my code这是我的代码

what happen?发生什么事?

const MyComponent = ({
  height,})=>{

 useEffect(() => {
    const getData = async () => {
      try {
        if (height < 10) {
          height = 10
        }
     const newValue = calculateValue(height, width)
    }
    if(isNotValidate){
       getData()
    }
}

by the error i am using useEffect but the error following apparing由于我正在使用 useEffect 的错误,但出现以下错误

In React, state and props are to be considered and treated as immutable.在 React 中,state 和 props 被认为是不可变的。 When you set height = 10 this mutates the props object.当您设置height = 10时,这会改变道具 object。 The error is saying that each render that the height prop is reset to the passed value.错误是说每个渲染height道具都重置为传递的值。

I just want to set new value if the current value is less to 10, and then calculateValue如果当前值小于10,我只想设置新值,然后calculateValue

You can use the maximum of height or the value 10.您可以使用最大height或值 10。

const newValue = calculateValue(Math.max(height, 10), width)

暂无
暂无

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

相关问题 来自 React Hook 内部的变量 useEffect 将在每次渲染后丢失 - Variable from inside React Hook useEffect will be lost after each render 每次渲染后,从 React Hook useEffect 内部对 'timeInterval' 变量的分配将丢失 - Assignments to the 'timeInterval' variable from inside React Hook useEffect will be lost after each render 如何修复:每次渲染后,React Hook useEffect 中对“主题”变量的赋值将丢失 - How to fix: Assignments to the 'theme' variable from inside React Hook useEffect will be lost after each render React Hook useEffect 缺少依赖项。 当没有任何东西损坏时,为什么我会收到此错误? - React Hook useEffect has a missing dependency. Why am I getting this error when nothing is broken? 我在使用 useEffect 和 React Redux 时遇到问题。 我在 useEffect 中收到错误说“没有从渲染中返回” - I am having an issue with useEffect and React Redux. I am getting errors within useEffect saying “nothing was returned from render” 为什么我在以下代码中收到此错误“反应无效的挂钩调用。只能在 function 组件的主体内部调用挂钩”? - Why am i getting this error "react Invalid hook call. Hooks can only be called inside of the body of a function component" in the following code? 当我尝试使用 react-table 钩子在 react 中渲染表时,出现未捕获错误:渲染器错误 - I am getting the Uncaught Error: Renderer Error when I try to render a table in react using react-table hook 为什么每次渲染后都会执行useEffect? - why is useEffect executing after each render? 我正在使用 UseEffect Hook,但组件仍然出现 Uncaught TypeError: Cannot set properties of null。 在反应 - I am using UseEffect Hook, but still component is getting Uncaught TypeError: Cannot set properties of null. in react 为什么我在 React 中收到无效的 Hook 调用? - why am i getting an Invalid Hook call in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM