Comments (1)
在第一个示例中,您使用了 useCallback 来创建一个稳定的回调函数 doThing,并将其作为依赖项传递给 useEffect。然而,由于您在 useCallback 的依赖项列表中没有指定任何依赖项,exhaustive-deps lint 规则会提示您缺少依赖项的警告。
在第二个示例中,您直接声明了一个普通的函数 doThing,它没有依赖项。由于它不是通过 useCallback 创建的回调函数,因此不需要显式指定依赖项,因此不会触发 exhaustive-deps lint 警告。
对于这种情况,您可以选择禁用 exhaustive-deps lint 规则,或者根据您的具体需求,将缺少的依赖项添加到 useCallback 的依赖项列表中。这取决于您对代码的预期行为和依赖项的管理方式。
关于您提到的建议,将具有空依赖列表的钩子视为“语义冻结”,这是一个可行的做法。如果您确定某个钩子的依赖项不会发生变化,并且您希望在组件的整个生命周期内保持固定的行为,您可以将空依赖列表视为一个标识,表示在任何依赖项变化时不重新执行该钩子。
from react.
Related Issues (20)
- Add `x_google_ignoreList` (Ignore-listing code) support to sourcemaps HOT 2
- Bug: useRef on react-test-renderer wont work HOT 1
- Bug: NOTHING BIG
- Bug: putting promise into JSX will cause hydration issue HOT 11
- Bug: Pressing `enter` on an input to submit the form submits the first button instead of the form itself (With Actions) HOT 5
- [DevTools Bug] Cannot remove node "434" because no matching node was found in the Store. HOT 1
- Bug: 40k modules in npm, fix that. PLEASE!!!!!
- [DevTools Bug]: Recent version broke modal in chrome browser HOT 1
- Bug: Uncaught TypeError: Cannot read properties of null (reading 'useContext') when importing react-router-dom HOT 25
- [DevTools Bug]: Console error on Firefox "moz-extension:*" pages
- [DevTools Bug]: Excessive CPU Usage HOT 4
- Feature: useEffectEvent should be useCallback without dependency array HOT 9
- Bug: Input icon prop is not working HOT 2
- Bug: React Context API Not Updating Correctly with Concurrent Mode HOT 2
- Reading order/sequence is not in understandable manner on Data card. HOT 1
- Feature: Release ReactCurrentDispatcher as a consumable API to be used in conjunction with useExternalSyncStore HOT 2
- Bug: dynamic import component which use useMemo internaly failed HOT 2
- Bug: Add LinkedIn Tab to website HOT 9
- Bug: HTML element <col> cannot be used as a direct descendant of <table> HOT 1
- Bug: Error in the react web page. (Go full-stack with a framework) HOT 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.