-
Notifications
You must be signed in to change notification settings - Fork 50k
Open
Labels
Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bug
Description
React version: [email protected], [email protected]
Steps To Reproduce
function getWidth(el: HTMLElement) {
return el.clientWidth;
}
function Component({value}: {value: string}) {
const [width, setWidth] = useState(0);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
setWidth(ref.current ? getWidth(ref.current) : 0);
// ^^^^^^^^^^^^ Avoid calling setState() directly within an effect
}, [value]);
return <div ref={ref}>{value}{width}</div>;
}Changing the line to setWidth(getWidth(ref.current)); makes the error go away, so it seems the presence of the ternary operator is confusing the rule.
The current behavior
Error is raised.
The expected behavior
No error to be raised.
Metadata
Metadata
Assignees
Labels
Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bug