Include detailed comments on few of the new React Hook examples
It's nice to see that react-spring already implemented React Hooks and further provides tons of examples! This is great and I highly appreciate your work.
But I still can't get a good understanding of how react-spring actually works. I am trying to implement a gallery with React Hooks where five pictures cross-fade infinitely. I took one of the new examples (the simple-transition one), adjusted it a bit and got something working but not really well. And tbh, I don't have a clue what is going on.
The code: https://codesandbox.io/s/0yv00j8j40
Some questions (and they show that I have not the slightest grasp):
- What's the difference between Spring and Transition? Can I also use Springs for my use case?
- The Transition doc says 'it takes a list of items of any type, and their keys', does 'list' implies an array? What is meant by 'keys'? The indexes of array items? What if I have just one item and no array?
- However, what are 'keys', for what does your Transition component needs them and why should my app care? What is meant by 'The latter defaults to item => item' in the Transition documentation?
- Could you elaborate on 'The full signature looks like (item, state, index) => props => ReactNode' with some examples? Where do you pass those args? In the examples I saw that you pass 'item', state as individual objects ('from'. 'enter', 'leave') and no index as key but as value from items; so it's not like in the documentation
- Does react-spring works together with CSS-in-JS generated CSS from modules like emotion?
It would be already a huge step for us if you could just include extensive comments in one or two examples and maybe write a post on the fundamentals of react-spring as a bonus (beyond the one tutorial post you just wrote). React-spring looks like an awesome lib and we need to fully understand the thoughts behind it in order to use it properly and not just copy and pasting snippets.
Here's your demo cleaned up: https://codesandbox.io/embed/morr206pv8
Now, your questions ...
-
Spring is just a basic value shifter, it goes from x 0 to x 1 if you will. A transition is different, it can retain objects when they are deleted and treat entering objects in a different way. You use that to animate lifecycles. For your control it's the right choice, you have image A on screen, when image B comes on you don't want to simply delete A, you want it to fade out. Transition handles that for you.
-
List is an array, yes. It can be any data. Sometimes the list you give it is self-descriptive, for instance [1,2,3,4,5] ---- if you don't supply keys it uses the object itself as a key. if you supply objects on the other hand: [{ id: 1, url: "..."},{ id: 2, url: "..."}], you need to teach it to recognize the keys: item => item.id
-
The key is important for it to know how to find items back.
-
transition uses render props, so your child is a function with 3 args: item, state and index. Usually you only need item, which is one of the items you have given it above (the "list", remember?). That function has to return a component. Components in react are structured: props => view. So there you have it, you receive props (which are the animated values) and you return a view. With hooks this is a bit different, because there are no render props.
-
Sure. I use styled-components, but it's the same:
const Button = styled(animated.button)`
...
`
@drcmda, many thanks for the quick response and cleaning up my code! This was super helpful: I didn't know the trick that function args keep their value in a closure and hence, I don't need useRef anymore. Also the item array looks much nicer now (was a long day yesterday).
One thing I noticed in Chrome Inspector is that the fading-out div are still in the DOM and kind of polluting it. Since I was not sure if this is intended or not, I forked your version and tested it with react-pose where the leaving divs are also removed from the DOM, look: https://codesandbox.io/s/2v783085j0
This was also a good opportunity to compare both modules briefly: Our slideshow example ran on react-spring constantly with 30fps while react-pose was jumping between 10 - 40 fps but there was no difference for the eye (both had the same windows size and where running independently). react-spring does allow to set the spring's tension and friction while react-pose does allow the stiffness only. However, you can also choose a tween instead of a spring with react-pose. Latter also allows to set different configs for enter and exit transitions. Regarding the API, I had to create my own map function with react-pose which makes the feeling a bit more natural but other than this both APIs provide flexibility.
Just wondering again: How does 'set' does know about the state in a closure? I just set the initial state to 2 instead of 0 and then, set knew about the '2' despite we haven't passed the initial state to set anywhere.
Edit: Ok, found it, it's a React Hooks feature: Functional updates: If the new state is computed using the previous state, you can pass a function to setState. The function will receive the previous value, and return an updated value https://reactjs.org/docs/hooks-reference.html#functional-updates
One thing I noticed in Chrome Inspector is that the fading-out div are still in the DOM and kind of polluting it. Since I was not sure if this is intended or not
No, that's a bug in the useTransition code. Good catch!
However, you can also choose a tween instead of a spring with react-pose
You can tween, though that's not our focus because tweeing is the main reason why animation on the web looks bad. See: http://react-spring.surge.sh/gotchas
If you do need it: config: { duration: 1000, easing: ... }
Latter also allows to set different configs for enter and exit transitions.
You can do this very easily. config can receive a function that gets all contextual info: the item, enter/leave/update, etc. Here's a very complex example doing this: https://codesandbox.io/embed/7mqy09jyq (config: (item, state) => (state === 'leave' ? ...).
Edit:
react-spring constantly with 30fps while react-pose was jumping between 10 - 40 fps but there was no difference for the eye
Would be strange if there was a difference. I tried it with the fixed demo and similar spring settings, and it's about the same, both get the same amount of scripting and fps. This is a very simple case, shouldn't really get taxing.
The bug is fixed, thanks @ruggedy !
https://codesandbox.io/embed/morr206pv8
@drcmda, nice, you released a new version, that was quick 👍
Closing this issue as the bug has been resolved in the latest releases
Latter also allows to set different configs for enter and exit transitions.
You can do this very easily. config can receive a function that gets all contextual info: the item, enter/leave/update, etc. Here's a very complex example doing this: https://codesandbox.io/embed/7mqy09jyq (
config: (item, state) => (state === 'leave' ? ...).
Good to know!