问题标签 [react-usememo]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
reactjs - 在 useEffect 中使用 memoized 对象时,我们可以使用 useMemo 吗?
想象一下这种情况。
接收对象并基于对象属性的钩子进行一些计算并返回结果。
使用钩子的组件将这些值分开,因此需要初始化一个新对象,以便将其传递给钩子。然后通过一些回调将钩子的结果传递回父级。
父级可以对该值进行一些其他计算,并将结果返回给子级。
当对象用作某些效果的依赖项时,在反应组件中处理对象创建的正确方法是什么?可以用useMemo
钩子吗?
官方文档说“你可能依赖 useMemo 作为性能优化,而不是语义保证。” https://reactjs.org/docs/hooks-reference.html#usememo
另一种方法是将钩子的道具从一个对象更改为简单的值,并从钩子的 useEffect 中的这些值创建对象,但这种方法不适合我。
javascript - useMemo hook always True even the location.pathname change
I am trying to render a specific Layout if the location change and I'm using the useMemo hook to validate the route and render the based on this value but it always returns true even the route change for example the new route is .../SSDSD/draft
react-hooks - 从 useMemo 的依赖项中省略其他钩子(例如 useContext 和 useNavigate)中的变量是否安全?
如果我忽略了这一点,我会收到一个 eslint 警告。但是,如果这会成为一个问题,因为它们会在连续渲染中发生变化,而不是 useMemo 中的渲染代码将在每个渲染上运行。
幸运的是,目前情况并非如此,但这可能会改变,不是吗?
那么最佳实践是什么?进行更正并将它们放在部门中,或者通过知道它永远安全而忽略警告?
reactjs - Is this valid syntax in React?
I am working on a React project and in there I have to initialize a socket once the component's been mounted. For that I am using this syntax and I want to know whether or not it is valid.
This way, my project and the sockets are working just fine but I want to know the conventional way to achieve the same. I used the same syntax in another React project and it gave me a "Reference Error" for the socket
variable.
reactjs - 警告:传递给 useMemo 的最后一个参数在渲染之间改变了大小。此数组的顺序和大小必须保持不变
当用户输入位置时,我会得到标记,然后将标记列表提供给 Google Maps 组件。因为我不想每次都重新渲染地图,所以我已经记住了它。它仅在标记道具更改时重新渲染。但是当标记发生变化时,我会收到以下警告:
我在其他帖子中遵循了答案,但没有一个对我有用。我究竟做错了什么?
我的地图组件:
react-usememo - 作为 value 属性传递给 Context 提供程序的对象在每次渲染时都会更改
任何人都可以帮我解决这个问题。我有这个错误:
src/index.js 第 9:36 行:作为 value 属性传递给 Context 提供者的对象(第 9 行)每次渲染都会改变。要解决此问题,请考虑将其包装在 useMemo 挂钩中 react/jsx-no-constructed-context-values
在这种情况下,我不确定如何使用 useMemo。
javascript - 访问 useMemo 中的最新状态
我正在使用react-quill
,它的要求是modules
道具必须被缓存并且不应该改变。我正在使用useMemo
钩子来记忆它。对象是:
它的用法如下:
handleFunc
在modules
对象中,只是控制台日志value
道具。问题是这value
不是最新的,它是第一个值。我在类组件中尝试了同样的事情,我能够获得价值并且它工作得非常好,但不幸的是它不适用于useMemo
. 请注意,我不能只输入as不应该更改[value]
的第二个参数。useMemo
modules
类组件实现:
但我不能使用类组件,因为它是只使用功能组件的要求。有什么办法,我可以有最新value
的useMemo
,或者任何其他的解决方案?
reactjs - 使用 redux-toolkit 的 useCallback 和 useMemo 是否有性能提升?
我已经看到很多关于为什么你不应该将 ReactuseCallback
与空的依赖列表一起使用的描述(这在逻辑上是有意义的),但我一直在试图弄清楚将它与redux-toolkit一起使用时是否有性能提升,当依赖列表包含 RT 对象时。(该应用程序对于一个用户来说已经足够高效,没有明显的差异,所以我希望总体上做“正确的事情”。)
一个例子:
以这种方式使用redux-toolkit ,记忆化我的回调和变量有什么好处吗?