avoidResettingAllStateWhenAPropChangesAvoid resetting all state when a prop changes. Instead, if "{{prop}}" is a key, pass it as "key" so React will reset the component's state.small-rules/no-reset-all-state-on-prop-changeDisallow resetting all state in an effect when a prop changes.
Ported from
eslint-plugin-react-you-might-not-need-an-effect/no-reset-all-state-on-property-change.
The upstream rule points at the “Resetting all state when a prop
changes”
section of the React docs, so that’s where the intent comes from.
This flags an effect that resets every piece of state back to its initial value, keyed off a prop change. That’s a remount you’re implementing by hand, and it’s worse than a remount because you get an extra render with the stale state first.
React already has the primitive for this: pass the prop as key and the component remounts with fresh
state, no effect required. It only works at the component boundary though, which is why the rule skips
custom hooks, a hook can’t receive a key.
avoidResettingAllStateWhenAPropChangesAvoid resetting all state when a prop changes. Instead, if "{{prop}}" is a key, pass it as "key" so React will reset the component's state.This rule accepts one options object after the severity.
import React, { useEffect, useState } from "@rbxts/react";
declare function recordView(): void;declare const utilities: { readonly trackView: () => void;};
export function ProfilePage({ userId }: { userId: string }): React.Element { const [user, setUser] = useState<string | undefined>(undefined); const [comment, setComment] = useState("type something"); recordView(); utilities.trackView();
useEffect(() => { setUser(undefined); setComment("type something"); }, [userId]);
return ( <frame> <textlabel Text={user} /> <textlabel Text={comment} /> </frame> );}import React, { useState } from "@rbxts/react";
export function ProfilePage(_properties: { key: string }): React.Element { const [comment, setComment] = useState("type something"); return <textbox Text={comment} TextChanged={(textbox: { readonly Text: string }) => setComment(textbox.Text)} />;}
export function Page({ userId }: { userId: string }): React.Element { return <ProfilePage key={userId} />;}