Skip to content

Commit 3941738

Browse files
committed
Merge branch 'dai-shi-update-jotai'
2 parents ee9057d + 1ce6e58 commit 3941738

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

frameworks/keyed/jotai/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/keyed/jotai/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"url": "https://github.com/krausest/js-framework-benchmark.git"
2323
},
2424
"dependencies": {
25+
"jotai": "1.3.3",
2526
"react": "17.0.1",
26-
"react-dom": "17.0.1",
27-
"jotai": "1.0.0"
27+
"react-dom": "17.0.1"
2828
},
2929
"devDependencies": {
3030
"@babel/core": "^7.14.5",

frameworks/keyed/jotai/src/main.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const createRowsAtom = atom(null, (_, set, amount: number) => {
1515

1616
const appendRowsAtom = atom(null, (_, set) => {
1717
set(dataAtom, (data) => data.concat(buildDataAtoms(1000)));
18-
set(selectedAtom, null);
1918
});
2019

2120
const updateRowsAtom = atom(null, (get, set) => {
@@ -32,7 +31,15 @@ const removeRowAtom = atom(null, (_, set, item: PrimitiveAtom<Data>) =>
3231
})
3332
);
3433

35-
const selectRowAtom = atom(null, (_, set, selected: PrimitiveAtom<Data>) => {
34+
const selectRowAtom = atom(null, (get, set, selected: PrimitiveAtom<Data>) => {
35+
const prevSelected = get(selectedAtom);
36+
if (prevSelected === selected) {
37+
return;
38+
}
39+
if (prevSelected) {
40+
set(prevSelected, (prev) => ({ ...prev, isSelected: false }))
41+
}
42+
set(selected, (prev) => ({ ...prev, isSelected: true }))
3643
set(selectedAtom, selected);
3744
});
3845

@@ -54,11 +61,10 @@ const GlyphIcon = (
5461

5562
interface RowProps {
5663
item: PrimitiveAtom<Data>;
57-
isSelected: boolean;
5864
}
5965

60-
const Row = memo<RowProps>(({ item, isSelected }) => {
61-
const [{ id, label }] = useAtom(item);
66+
const Row = memo<RowProps>(({ item }) => {
67+
const [{ id, label, isSelected }] = useAtom(item);
6268
const selectRow = useUpdateAtom(selectRowAtom);
6369
const removeRow = useUpdateAtom(removeRowAtom);
6470
return (
@@ -78,14 +84,12 @@ const Row = memo<RowProps>(({ item, isSelected }) => {
7884

7985
const RowList = memo(() => {
8086
const [data] = useAtom(dataAtom);
81-
const [selected] = useAtom(selectedAtom);
8287
return (
8388
<>
8489
{data.map((item) => (
8590
<Row
8691
key={String(item)}
8792
item={item}
88-
isSelected={item === selected}
8993
/>
9094
))}
9195
</>

frameworks/keyed/jotai/src/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ let nextId = 1;
1313
export interface Data {
1414
id: number;
1515
label: string;
16+
isSelected?: boolean;
1617
}
1718

1819
export function buildDataAtoms(count: number): PrimitiveAtom<Data>[] {
1920
const data = new Array(count);
2021
for (let i = 0; i < count; i++) {
2122
data[i] = atom({
22-
id: nextId++,
23-
label: `${A[random(A.length)]} ${C[random(C.length)]} ${
24-
N[random(N.length)]
25-
}`,
23+
id: nextId++,
24+
label: `${A[random(A.length)]} ${C[random(C.length)]} ${
25+
N[random(N.length)]
26+
}`,
2627
});
2728
}
2829
return data;

0 commit comments

Comments
 (0)