-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: useTransition rewrite #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
TodoQuestions
Ideas
|
78bc5a5
to
672393e
Compare
Feature proposal for the new
A real-world use case for (1) above is when animating an interactive chart. Use a Spring value to animate the chart's x/y axis, while // Pass the animated value `x` to the `expires` prop of transition,
// so that the unmount event can wait for the X-axis animation to rest,
// instead of in the middle of X-axis animation.
const [{ x }, setAxis] = useSpring(() => { x: 100 });
const transition = useTransition(elements, { ...props, expires: [x] }); |
7ff0d8d
to
0d71b81
Compare
423592a
to
bef8d1c
Compare
d6baddb
to
1854f36
Compare
f9ab364
to
4dea3e1
Compare
1854f36
to
1a2be58
Compare
aa2db98
to
6b3a7c0
Compare
@aleclarson My apologies. Don't know why I thought I was linking a new sandbox. I am indeed working from the code you linked. I cannot prevent it from running the last line in the If I comment out that line, then |
@aleclarson Just now saw your edit. I tried it, but it still runs
|
@Lynges Yeah that's a bug I'm looking into now. My suggested edit was still wrong though, for other reasons. Give me a minute to figure this out. :) |
@Lynges Ok, sorry for the wait. Things got a little tricky (and I was distracted by the React Conf stream 😜), but 6 fixes later, everything should work as expected. Add the following prop to the onRest: ({ finished }) => finished && cancelMap.get(item)() Note: You'll need to install
|
Thanks a bunch @aleclarson :) Got it all working. Found a couple of things with regards to when the components are re-rendered. I will do some more digging on Monday to see if it's just my code or something that might be relevant. |
I still don't understand how to toggle with new useTransition
|
@dellwatson Hi, the following should work if you pass the proper index but there might be better options : const items = ['😄',' 🤪'] export default () => {
const [toggled, setToggled] = useState(false)
const transition = useTransition(items[+toggled], {
from: { opacity: 0 },
enter: { opacity: 1 },
})
return (
<>
<button onClick={() => setToggled(!toggled)}>Switch please</button>
<div className="simple-trans-main">
{transition((values, item) => (
<animated.div style={values}>{item}</animated.div>
))}
</div>
</>
)
} Live example here : https://codesandbox.io/s/eager-cerf-r78kw |
Does |
@Inviz Which version are you using? |
Hi @aleclarson, I am trying the latest canary. My question : is there any effect if I set the I want to reverse the transition animation from last item to first item instead of first item to last item like usual and it seems that option is not working at all Thank you in advance! |
This PR is a rewrite of
useTransition
from scratch in hopes of simplifying both the API and the internals. Here's an example of the new API:Once #670 is merged, you can pass a deps array and you'll get the
update
andstop
functions returned to you:Also, the
Transition
component has changed. Itschildren
prop now takes different arguments:Notable changes
keys
argument is now thekey
propkey
prop is optional for primitive items and mutable object itemstransition
function instead of atransitions
arraykey
prop set for you automaticallyphase
isn't exposed to the user anymoreunique
prop was removed for simplification)lazy
prop was removed in favor ofexpires
)props.onFrame
), but you can still pass them in specific phases (eg:props.enter.onFrame
)reset
prop now uses theinitial
prop if it existsonDestroyed
prop was removed (was there a good use case for this?)order
prop was removedNew
key
propAlias:
keys
When immutable objects are passed as items, they need explicit keys in order to reconnect them with any existing transitions.
For an array of immutable objects, pass a function that maps over the array and returns the unique id of each item...
... or pass an array of keys.
For a single immutable object, you can pass its key directly (no function required).
New
expires
propFor controlling when deleted items are dismounted after their leave animation finishes. Set to
0
for immediate dismount. By default, dismounting is postponed until (1) next render, or (2) all transitions are resting. Any value above0
is interpreted as "milliseconds to wait before dismounting is forced".