Skip to content

Mutable values in render #23

Description

@oliverjam

let popup = undefined;

If you need to have a variable that you can mutate and maintain the value of across renders (e.g. popup here) React has a hook called useRef. It's designed for values that you don't want to recreate on every render—kind of like opting out of the state-based render cycle.

So this

const popup = React.useRef(null);
// ...
console.log(popup.current)

If you just use a normal variable like you've got then it'll get recreated whenever this component re-renders, which you probably don't want.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions