简体   繁体   English

React.js 将 setState 作为 prop 传递会导致警告有关将 props 用于依赖项

[英]React.js Passing setState as prop causes a warning about using props for dependencies

I am passing state as a variable down to a component via props like to...我通过类似道具将 state 作为变量传递给组件,例如...

const [someState, setSomeState] = useState([])
PlaceDataInIndexDB({ setSomeState: setSomeState,
    user: props.user })

And in the PlaceDataInIndexDB.js I have a useEffect which eventually sets the state using在 PlaceDataInIndexDB.js 我有一个 useEffect 最终设置 state 使用

useEffect(() => {
    props.setSomeState([array])
}), [props.user]
 

The issue is that I get a warning saying I need to use props in my dependency array, however, I do not want the useEffect to run every time props change because I have a lot of other props in there.问题是我收到警告说我需要在我的依赖项数组中使用道具,但是,我不希望每次道具更改时都运行 useEffect,因为那里有很多其他道具。 What can I do for this?我能为此做些什么? This is an extremely simplified version of my code.这是我的代码的一个极其简化的版本。 i can post exact code but it is a lot more to look through.我可以发布确切的代码,但要查看的内容要多得多。

And here is the warning...这是警告...

React Hook useEffect has a missing dependency: 'props'. React Hook useEffect 缺少依赖项:'props'。 Either include it or remove the dependency array.包括它或删除依赖数组。 However, 'props' will change when any prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect react-hooks/exhaustive-deps但是,当任何道具发生变化时,“道具”会发生变化,因此首选的解决方法是在 useEffect 调用之外解构“道具” object,并在 useEffect react-hooks/exhaustive-deps 中引用这些特定道具

It's telling you what the issue is.它告诉你问题是什么。 Generally, anything referenced inside of the useEffect function needs to also exist in the dependency array, so React knows to run it again when those things change.通常, useEffect function 中引用的任何内容也需要存在于依赖数组中,因此 React 知道在这些事情发生变化时再次运行它。 If the thing you're using is a property of some other object (like props ), it's best to pull the values out of there prior to referencing them.如果您使用的东西是其他一些 object 的属性(如props ),最好在引用它们之前从那里提取值。

const PlaceDataInIndexDB = (props) => {
  const { setSomeState, user } = props

  useEffect(() => {
    setSomeState([array])
  }), [setSomeState, array] // <-- your example doesn't show where `array` comes from, but needed here as well

  // ...
}

You can, in fact, destructure the props inline so that you never even have a reference to the entire props object:事实上,你可以内联解构 props,这样你甚至都不会引用整个props object:

const PlaceDataInIndexDB = ({ setSomeState, user }) => {

Note that setSomeState must also be in the dependency array.注意setSomeState也必须在依赖数组中。 If -- and only if -- the useState is in the same component, the linter is smart enough to know that it never changes and lets you leave it out.如果 - 并且仅当 - useState在同一个组件中,linter 足够聪明,可以知道它永远不会改变,并让您将其排除在外。 However, if it's passed in as a prop from somewhere else, there is no way for it to know.但是,如果它是从其他地方作为道具传入的,它就无法知道。

The linting rules for hooks are very good.钩子的 linting 规则非常好。 By and large, unless you really know what you're doing, you can blindly follow the suggestions it makes.总的来说,除非你真的知道自己在做什么,否则你可以盲目地听从它提出的建议。 Eslint actually added an entire "suggestions" feature for this specific rule, which eg vscode will change for you if you put your cursor on the error and press ctrl+. Eslint 实际上为此特定规则添加了一个完整的“建议”功能,例如,如果您将 cursor 放在错误上并按ctrl+.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM