Telegram Group & Telegram Channel
🔥 React: избегай лишнего состояния с derived state!

В React часто встречается антипаттерн — derived state — когда ты дублируешь вычисляемое значение в useState.

👎 Пример плохого подхода:


const [items, setItems] = useState([]);
const [filtered, setFiltered] = useState([]);

useEffect(() => {
setFiltered(items.filter(...));
}, [items]);


🔍 Проблема: ты сам обязан синхронизировать filtered с items. Это источник багов, особенно при сложных зависимостях.

Лучше вычисляй на лету:


const filtered = useMemo(() => items.filter(...), [items]);


🔧 useMemo кэширует результат, и ты не хранишь дублирующее состояние.

🧠 Правило: если значение можно вычислить из другого — не пиши useState.

Подробнее: https://react.dev/learn/you-might-not-need-an-effect

✍️ @React_lib



tg-me.com/React_lib/676
Create:
Last Update:

🔥 React: избегай лишнего состояния с derived state!

В React часто встречается антипаттерн — derived state — когда ты дублируешь вычисляемое значение в useState.

👎 Пример плохого подхода:


const [items, setItems] = useState([]);
const [filtered, setFiltered] = useState([]);

useEffect(() => {
setFiltered(items.filter(...));
}, [items]);


🔍 Проблема: ты сам обязан синхронизировать filtered с items. Это источник багов, особенно при сложных зависимостях.

Лучше вычисляй на лету:


const filtered = useMemo(() => items.filter(...), [items]);


🔧 useMemo кэширует результат, и ты не хранишь дублирующее состояние.

🧠 Правило: если значение можно вычислить из другого — не пиши useState.

Подробнее: https://react.dev/learn/you-might-not-need-an-effect

✍️ @React_lib

BY React




Share with your friend now:
tg-me.com/React_lib/676

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

To pay the bills, Mr. Durov is issuing investors $1 billion to $1.5 billion of company debt, with the promise of discounted equity if the company eventually goes public, the people briefed on the plans said. He has also announced plans to start selling ads in public Telegram channels as soon as later this year, as well as offering other premium services for businesses and users.

Why Telegram?

Telegram has no known backdoors and, even though it is come in for criticism for using proprietary encryption methods instead of open-source ones, those have yet to be compromised. While no messaging app can guarantee a 100% impermeable defense against determined attackers, Telegram is vulnerabilities are few and either theoretical or based on spoof files fooling users into actively enabling an attack.

telegram from us


Telegram React
FROM USA