question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A new hook for update animations

See original GitHub issue

Problem

For SVG elements that need animation, a ‘hook’ is needed to tween the element from one configuration to another before React steps in and updates the DOM (and thereby prevents the animation).

My current work-around

For SVG components that need animation, I have to write render method that always returns the same ‘dummy’ DOM so that React doesn’t find any diffs and I can handle all DOM updates myself inside the componentDidUpdate. IMO, this results in a very non-idiomatic ReactJS code because the render method does not have a true representation of what the SVG element will look like after applying state and props. See this example.

Possible Solution

It would be helpful if components inside transition groups had a componentWillTween(callback) method similar to componentWillEnter etc.

This would be invoked after componentWillUpdate but before componentDidUpdate for components added to an existing TransitionGroup. It would block updates from being flushed to DOM until the callback is called. It would not be called on the initial render of a TransitionGroup.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
levibuzoliccommented, May 6, 2015

Definitely don’t setState in componentDidUpdate that will result in an infinite loop.

Use setState in a timing function which will update state that will modify your render. It’s not hacky, it’s what React is made for.

You also won’t need to duplicate much/any of D3’s code, you just need a timing function.

Your render will end up looking something like:

<rect
  className="bar"
  x={ this.state.progress * this.props.x }
  y={ this.state.progress * this.props.y }
  width={ this.state.progress * this.props.width }
  height={ this.state.progress * this.props.height }
  fill={this.props.color} // color is a little trickier, you'll need a library or method that can generate a colour based on a start/end point and progress.
/>
0reactions
sophiebitscommented, May 29, 2015

Yes, @levibuzolic’s approach sounds great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Animations with React Hooks - Medium
How do we update each animation state based on our toggle value? Introducing, useEffect() . This hook combines the power of lifecycle ...
Read more >
Animations using React Hooks and GreenSock
Learn how to implement powerful animations using React Hooks and GreenSock in this tutorial, which covers basic and advanced concepts.
Read more >
Animations | React Native Reanimated - Software Mansion
Interruptions also work correctly for animations defined in useAnimatedStyle hook. When the style is updated and the target value for a given property...
Read more >
Upgrading to React Hooks: Parcel, Web Animations ... - ITNEXT
In order for the resize event to work properly, it has to update some component state to trigger a render… Otherwise, the canvas...
Read more >
Using requestAnimationFrame with React Hooks - CSS-Tricks
Why is it bad? If you run this, the useEffect will trigger the animate function that will both change the state and request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found