Skip to content

Instantly share code, notes, and snippets.

@notsidney
Last active October 5, 2021 04:21
Show Gist options
  • Save notsidney/2fa3f77a4c068322528aa897a7ae75d5 to your computer and use it in GitHub Desktop.
Save notsidney/2fa3f77a4c068322528aa897a7ae75d5 to your computer and use it in GitHub Desktop.
How to useRef to Fix React Performance Issues — Gists
import { SideDrawerRef } from 'SideDrawer'
export function ProjectContextProvider({ children }) {
const sideDrawerRef = useRef<SideDrawerRef>();
return (
<ProjectContext.Provider value={{ sideDrawerRef }}>
{children}
</ProjectContext.Provider>
)
}
export type SideDrawerRef = {
cell: SelectedCell;
setCell: React.Dispatch<React.SetStateAction<SelectedCell>>;
open: boolean;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
};
export default function SideDrawer() {
const classes = useStyles();
const { tableState, dataGridRef, sideDrawerRef } = useProjectContext();
const [cell, setCell] = useState<SelectedCell>(null);
const [open, setOpen] = useState(false);
sideDrawerRef.current = { cell, setCell, open, setOpen };
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment