From 12ca0d43279253543790b48d9bb5dd0da218cb56 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 4 Feb 2022 20:49:41 +0000 Subject: [PATCH] Mention loops --- beta/src/pages/apis/usestate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beta/src/pages/apis/usestate.md b/beta/src/pages/apis/usestate.md index 9c3770ea..fefe8ac5 100644 --- a/beta/src/pages/apis/usestate.md +++ b/beta/src/pages/apis/usestate.md @@ -854,6 +854,6 @@ button { margin-bottom: 10px; } -In this example, if you can't modify the parent component to track it, derived state is the right solution. +Note that derived state must set be inside a condition like `prevCount !== count`, and there must be a call like `setPrevCount(count)` inside of it. Otherwise, your component would re-render in a loop until it crashes. When you can, try to avoid derived state. However, derived state is better than updating state in an effect. This is because calling the `set` function in render restarts a render immediately without waiting for child components.