setValue() has no effect before first render
See original GitHub issueIt 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:
- Created 4 years ago
- Comments:12 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’re also seeing this behavior.
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.
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.