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.

Bug: incorrect `interpolatingStyle` value for `Motion` after `onRest`

See original GitHub issue

Example: http://www.webpackbin.com/N16jKp-cz

import React, {PureComponent } from 'react';
import {render} from 'react-dom';
import {Motion, spring} from 'react-motion';

class App extends PureComponent {
  constructor(...args) {
    super(...args);
    
    this.state = {
      x: spring(10)
    }
    
    this.onRest = this.onRest.bind(this);
  }
    
  onRest() {
   // changing to a number value
    this.setState({
      x: 100
    });
  }
  
  render() {
    
    return (
      <Motion defaultStyle={{x: 0}} style={this.state} onRest={this.onRest}>
        {interpolatingStyle => {
          return (
            <div>
              {/* on final render this should be '100' but it is the last spring value: '10' */}
              interpolated x: {interpolatingStyle.x} <br/>
              {/* on final render this renders '100' as expected */}
              current x: {JSON.stringify(this.state.x)}
            </div>
          )
        }}
      </Motion>
    )
  }
}

render(<App />, document.getElementById('app'));

Workaround: put update on the end of the stack

onRest() {
    setTimeout(() => {
      this.setState({
        x: 100
      });
    });
  };

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
nkbtcommented, Mar 21, 2017

Yeah setTimeout is necessary to use with onRest. I reckon this has something to do with async nature/batching of setState in React.

We can probably switch to setState(pureCallback) notation as now recommended. Hopefully it would eliminate the problem.

You may check https://github.com/nkbt/react-motion-loop with my workaround, which is the same…

0reactions
nkbtcommented, Mar 21, 2017

if you check source of react-collapse@3 you will find fairly ugly but working solution with instance vars. I had to figure out when animation ends properly and comparing with end values is not the right one, since spring can go into negative values in many cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

onRest seems to cause React-Motion to ignore rerenders #322
But to make it work I have to wrap setState in onRest callback with setTimeout , otherwise new step value is ignored in...
Read more >
How to fix "that" problem in After Effects - YouTube
I know I know. You've probably figured this out by now, but it took me a few months to know why After Effects...
Read more >
react-motion | Yarn - Package Manager
Specifies both the rounding of the interpolated value and the speed (internal). It's normal not to feel how stiffness and damping affect your...
Read more >
Robotic Motion Planning: Bug Algorithms
A boundary following behavior invoked when heuristic distance increases. • A value d min which is the shortest distance observed thus far between...
Read more >
Stopping false positive motion triggered by bugs attracted to ...
I set up four G3 Pro and one G4 cameras for outdoor monitoring and I'm finding the Unifi Protect software not as great...
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