Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

> > @monsterooo #41

Open
zsjun opened this issue Jun 18, 2020 · 0 comments
Open

> > @monsterooo #41

zsjun opened this issue Jun 18, 2020 · 0 comments

Comments

@zsjun
Copy link

zsjun commented Jun 18, 2020

@monsterooo

我同意@AlexZhong22c的说法。

  • 这个依赖是要写的,useCallback只是说当依赖不变的时候,返回同一个函数,但是不能省略依赖的
  • useCallback应该是说避免了其他的state对当前函数的影响。这个count的依赖项我认为是必须要的。
  • 另外呢,还可以用useReducer去维护一个局部的redux,dispatch去做,见useReducer

我看错了, setCount(count => count + 1) setCount是个函数,可以拿到最新的值。相当于以前的setState(prevState => prevState.count + 1)。
不过,useCallback(() => setCount(count => count + 1), [])第二个参数写空数组不合适的吧,毕竟官方文档描述认为函数中引用的项都应该出现在依赖中

最佳实践还是按照官方的来没问题,因为那个比较适合大多数情况。

像我们讨论的这种情况(优化依赖和缓存函数),不是应用特别大或者组件特别多的时候不会有太大的问题。

但是如果我们在写代码之前就知道并且做一些优化,对于以后可能会出现的性能问题就会有所避免。

Originally posted by @monsterooo in #37 (comment)

react 推荐的官方插件,会强制给你把count给你添加上,还有一点gif图,如果不开全局代理,gif图都挂了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant