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

Question: memo + useState #15661

Closed
python273 opened this issue May 15, 2019 · 1 comment
Closed

Question: memo + useState #15661

python273 opened this issue May 15, 2019 · 1 comment

Comments

@python273
Copy link

python273 commented May 15, 2019

Does it make sense to use memo + useState?

const Dunno = memo(({a}) => {
  const [b, setB] = useState(0);
...

If it doesn't, why the component updates when setB is called? 🤔


https://stackblitz.com/edit/react-dxosdw
https://reactjs.org/docs/react-api.html#reactmemo

If your function component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result.

@threepointone
Copy link
Contributor

state is internal to a component, so it's allowed to update itself. memo does its memoizing on incoming props, not on state.

(that said, if you call setB with an unchanged value (for example, in your snippet, if you call setB(0)), the component will bail out of rendering since it knows that state hasn't changed)

closing this issue since it isn't a bug or feature request.

yuche added a commit to NervJS/taro that referenced this issue Jul 19, 2019
函数式组件中只有使用 memo 才会存在 SCU 函数,hooks 更新组件时,注入一个内部函数变量作为回调,
当 setState 回调和这个内部函数变量一致时可以触发无视 SCU 的更新。
除此之外此次更新策略仍然保持和普通 setState 更新一致,把数据合并到 nextTick 再统一更新
此次 commit 应用到所有小程序端.
ref: facebook/react#15661
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

2 participants