How to avoid extra rerender?
See original GitHub issuewith Jotai, the <FakeCard />
will re-render whenever editId changed, but Recoil.js does not because i can select only setter function. How to achieve the same behavior with Jotai?
Recoil
import React from 'react';
import { atom, useRecoilState, useSetRecoilState } from 'recoil';
const editIdAtom = atom<number | null>({
key: `editIdAtom`,
default: null,
});
function FakeCard() {
const setEditId = useSetRecoilState(editIdAtom);
const handleClick = React.useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
setEditId(1);
},
[setEditId]
);
return (
<div>
<a onClick={handleClick}>toggle</a>
<FakeModal />
</div>
);
}
function FakeModal() {
const [editId, setEditId] = useRecoilState(editIdAtom);
const handleClose = React.useCallback(() => {
setEditId(null);
}, [setEditId]);
return editId ? <span>editing {editId}</span> : null;
}
Jotai
import React from 'react';
import { atom, useAtom } from 'jotai';
const editIdAtom = atom<number | null>(null);
function FakeCard() {
const [,setEditId] = useAtom(editIdAtom);
const handleClick = React.useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
setEditId(1);
},
[setEditId]
);
return (
<div>
<a onClick={handleClick}>toggle</a>
<FakeModal />
</div>
);
}
function FakeModal() {
const [editId, setEditId] = useAtom(editIdAtom);
const handleClose = React.useCallback(() => {
setEditId(null);
}, [setEditId]);
return editId ? <span>editing {editId}</span> : null;
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top Results From Across the Web
Optimizing React performance by preventing unnecessary ...
Only re-rendering React components when necessary can make app your faster. This article explains how to reduce re-renders and avoid common ...
Read more >How to avoid multiple re-renders in React (3 lines of code)
To avoid re-rendering per component with the you will use the shouldComponentUpdate() lifecycle . First, if you're looking to become a strong ...
Read more >How to prevent re-rendering of components that have not ...
I would like to prevent the re-rendering and only re-render the component that actually changes. I have seen that useCallback is the right...
Read more >Just Say No to Excessive Re-Rendering in React
To prevent excessive re-rendering, move the expensive component to a parent component, where it will render less often, and then pass it down...
Read more >4 options to prevent extra rerenders with React context
4 options to prevent extra rerenders with React context · Introduction · Base example · Option 1: Split contexts · Option 2: React.memo...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi, thanks for opening up the issue. Jotai core doesn’t provide
useSetRecoilState
equivalent. Instead, we use write-only atoms.Now, I wonder if we can build a generalized hook.
No. 😁 https://github.com/pmndrs/jotai/blob/84802077db94ee8874a7e61422e47662d6db7ed6/src/utils.ts#L2