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.

setValue() has no effect before first render

See original GitHub issue

It seems like setValue() call before render() function (before the animated value got bind to the UI) has no effect. See repro code below. Basically I created an animated value with 0 as initial value, right before rendering I use setValue to change it’s value to 100 and the value does not change (both from UI and debug log) But if I wait for 0ms and set value, then the value got updated.

This is tested on 1.20. I’m still testing 1.30, but it seems worse (even with 100 ms delay the value still not got updated)

const animatedValue = new Animated.Value<number>(0)
const Comp: TestCompoment = p => {
  // >>> this does NOT work, log: <<<
  // > animatedValue.setValue(100)
  // > animatedValue: 0
  console.log('animatedValue.setValue(100)')
  animatedValue.setValue(100)

  // >>> Code below works, with log: <<<
  // > animatedValue.setValue(100)
  // > animatedValue: 0
  // > animatedValue: 100
  // setTimeout(() => {
  //   console.log('animatedValue.setValue(100)')
  //   animatedValue.setValue(100)
  // }, 0)
  return <View style={StyleSheet.absoluteFill}>
    <Animated.View style={{
      width: 100,
      height: 100,
      backgroundColor: 'red',
      transform: [
        {translateX: debug('animatedValue:', animatedValue)},
      ],
    }}/>
  </View>
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Ashoatcommented, Jun 11, 2021

We’re also seeing this behavior.

I see this case, but I cannot think of any legitimate use case.

There are plenty of legitimate use cases! In our case, we don’t always mount the animated view in question, but we want the value to be correct when we mount it.

It seems pretty clear to me that this is buggy behavior.

0reactions
harveyconnorcommented, May 19, 2021

I’m getting a similar issue trying to manually setValue whilst utilising a panHandler to manage the value too. The value ends up getting messed up when I start panning after manually setting and results in a jump.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: How to skip useEffect on first render - Stack Overflow
The first effect is the main one as if you were using it in your component. It will run, discover that isMounted isn't...
Read more >
How to Use localStorage with React Hooks to Set and Get Items
The useEffect() hook helps us fetch all items on first render, which means that when the component mounts or re-renders, it obtains all...
Read more >
How the useEffect Hook Works (with Examples) - Dave Ceddia
After rendering finishes, useEffect will check the list of dependency values against the values from the last render, and will call your effect...
Read more >
Hooks | Preact: Fast 3kb React alternative with the same ES6 ...
Hooks is a new concept that allows you to compose state and side effects. ... with Preact for a while, you may be...
Read more >
React setState does not immediately update the state - Medium
State right after calling setState will have value before the update. const [value, setValue] = useState("");setValue("React is awesome!
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