-
-
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): updating an array of objects often leads to a…
- Loading branch information
1 parent
2e73e79
commit 529236a
Showing
2 changed files
with
84 additions
and
19 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,26 +1,57 @@ | ||
import { useState } from 'react' | ||
import { SwitchTransition } from 'transition-hooks' | ||
import { useListTransition } from 'transition-hooks' | ||
// import { startViewTransition } from 'transition-hooks/viewTransition' | ||
|
||
export function App() { | ||
const [show, setShow] = useState(false) | ||
const [list, setList] = useState([ | ||
{ id: 1, text: '1' }, | ||
{ id: 2, text: '2' }, | ||
]) | ||
|
||
const { transitionList } = useListTransition(list, { | ||
timeout: 1000, | ||
keyExtractor: i => i.id, | ||
}) | ||
|
||
const updateLastItem = () => { | ||
const newList = [...list] | ||
newList[newList.length - 2] = { | ||
id: newList[newList.length - 2].id, | ||
text: `${newList[newList.length - 2].text}test`, | ||
} | ||
setList(newList) | ||
} | ||
|
||
const addItem = () => { | ||
setList(l => [...l, { id: l.length + 1, text: (l.length + 1).toString() }]) | ||
} | ||
|
||
return ( | ||
<div> | ||
<SwitchTransition state={show}> | ||
{(state, { status }) => { | ||
<div style={{ display: 'flex', gap: 4, marginBottom: 8 }}> | ||
<button onClick={addItem}>add</button> | ||
<button onClick={updateLastItem}>update last item</button> | ||
</div> | ||
<ul> | ||
{transitionList((item, { key, simpleStatus }) => { | ||
return ( | ||
<div | ||
<li | ||
style={{ | ||
transition: 'opacity 0.3s', | ||
opacity: status === 'entering' || status === 'entered' ? 1 : 0, | ||
position: simpleStatus === 'exit' ? 'absolute' : 'relative', | ||
opacity: simpleStatus === 'enter' ? 1 : 0, | ||
transform: | ||
simpleStatus === 'enter' | ||
? 'translateX(0)' | ||
: 'translateX(20px)', | ||
transition: 'all .6s', | ||
// viewTransitionName: simpleStatus === 'enter' ? `transition-list-${key}` : '', | ||
}} | ||
> | ||
Hello Word {state ? 'true' : 'false'} | ||
</div> | ||
- {item.text} | ||
</li> | ||
) | ||
}} | ||
</SwitchTransition> | ||
<button onClick={() => setShow(!show)}>toggle</button> | ||
})} | ||
</ul> | ||
</div> | ||
) | ||
} |
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