-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: uselisttransition entered error
- Loading branch information
1 parent
efd505a
commit 4122a63
Showing
2 changed files
with
27 additions
and
14 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,34 +1,48 @@ | ||
import { useRef, useState } from 'react' | ||
import { useListTransition } from 'transition-hooks' | ||
import { getSimpleStatus, useListTransition } from 'transition-hooks' | ||
import { Button } from '../Button' | ||
|
||
const numbers = Array.from({ length: 5 }, (_, i) => i) | ||
export function BasicUseListTransition() { | ||
const [list, setList] = useState(numbers) | ||
const idRef = useRef(numbers.at(-1)! + 1) | ||
const transition = useListTransition(list) | ||
const idRef = useRef(numbers.length) | ||
const transition = useListTransition(list, { entered: false, timeout: 300 }) | ||
|
||
return ( | ||
<div> | ||
<Button onClick={insert}>insert</Button> | ||
<Button onClick={remove}>remove</Button> | ||
<div style={{ display: 'flex', gap: 4 }}> | ||
<Button onClick={insert}>insert</Button> | ||
<Button onClick={remove}>remove</Button> | ||
</div> | ||
<ul> | ||
{transition((item, { status }) => { | ||
// console.log('🚀 ~ {transition ~ item:', item) | ||
const simpleStatus = getSimpleStatus(status) | ||
return ( | ||
<li key={item}>{item}</li> | ||
<li | ||
style={{ | ||
opacity: simpleStatus === 'enter' ? 1 : 0, | ||
transform: simpleStatus === 'enter' ? 'translateX(0)' : 'translateX(20px)', | ||
transition: 'all 300ms', | ||
}} | ||
> | ||
- {item} | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
{} | ||
</div> | ||
) | ||
function insert() { | ||
const i = Math.round(Math.random() * list.length) | ||
setList([...list].splice(i, 0, idRef.current++)) | ||
const newList = [...list] | ||
newList.splice(i, 0, idRef.current++) | ||
setList(newList) | ||
} | ||
function remove() { | ||
const i = Math.round(Math.random() * list.length - 1) | ||
|
||
setList([...list].splice(i, 1)) | ||
const newList = [...list] | ||
newList.splice(i, 1) | ||
setList(newList) | ||
} | ||
} |
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