-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,87 @@ | ||
import { useEffect, useState, useRef } from "react"; | ||
import { atom, useAtom } from "jotai"; | ||
import { useEffect, useState, useRef } from 'react' | ||
import { atom, useAtom } from 'jotai' | ||
import { | ||
useAtomWithSchedule, | ||
NormalPriority, | ||
ImmediatePriority, | ||
} from "jotai-scheduler"; | ||
} from 'jotai-scheduler' | ||
|
||
const anAtom = atom(0); | ||
const anAtom = atom(0) | ||
|
||
const simulateHeavyRender = () => { | ||
const start = performance.now(); | ||
const start = performance.now() | ||
while (performance.now() - start < 10) {} | ||
}; | ||
} | ||
|
||
function useIsVisible() { | ||
const ref = useRef(null); | ||
const [isVisible, setIsVisible] = useState(false); | ||
const ref = useRef(null) | ||
const [isVisible, setIsVisible] = useState(false) | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
([entry]) => { | ||
setIsVisible(entry.isIntersecting); | ||
} | ||
); | ||
const observer = new IntersectionObserver(([entry]) => { | ||
setIsVisible(entry.isIntersecting) | ||
}) | ||
|
||
if (ref.current) { | ||
observer.observe(ref.current); | ||
observer.observe(ref.current) | ||
} | ||
|
||
return () => { | ||
if (ref.current) { | ||
observer.unobserve(ref.current); | ||
observer.unobserve(ref.current) | ||
} | ||
}; | ||
}, [ref]); | ||
} | ||
}, [ref]) | ||
|
||
return [ref, isVisible] as const; | ||
return [ref, isVisible] as const | ||
} | ||
|
||
const Item = () => { | ||
simulateHeavyRender(); | ||
const [ref, isVisible] = useIsVisible(); | ||
simulateHeavyRender() | ||
const [ref, isVisible] = useIsVisible() | ||
// const [num, setNum] = useAtom(anAtom); | ||
const [num, setNum] = useAtomWithSchedule(anAtom, { | ||
priority: isVisible ? ImmediatePriority : NormalPriority, | ||
}); | ||
}) | ||
return ( | ||
<div | ||
ref={ref} | ||
style={{ | ||
height: "50px", | ||
width: "50%", | ||
margin: "10px", | ||
textAlign: "center", | ||
border: "2px solid black", | ||
height: '50px', | ||
width: '50%', | ||
margin: '10px', | ||
textAlign: 'center', | ||
border: '2px solid black', | ||
}} | ||
> | ||
<div> | ||
{num} {isVisible ? "visible" : "not visible"} | ||
{num} {isVisible ? 'visible' : 'not visible'} | ||
</div> | ||
<button | ||
onClick={() => { | ||
setNum(num + 1); | ||
setNum(num + 1) | ||
}} | ||
> | ||
+1 | ||
</button> | ||
</div> | ||
); | ||
}; | ||
) | ||
} | ||
|
||
export const App = () => { | ||
const items = new Array(100).fill(0); | ||
const items = new Array(100).fill(0) | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center", | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
}} | ||
> | ||
{items.map(() => ( | ||
<Item /> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
) | ||
} |