Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Disable the button during the animation in React-Native

I'm trying to create an app that starts animation after a button click and disables the button while the animation is running. I want to be able to use it multiple times by pressing the button, disabling it, running animation, then reenabling it. So far, I have a button that gets disabled when I press it and then after that I run an animation (blue square moves to the right and then back to starting position), or at least that's what I was hoping for. The reality of the situation is that when a button is pressed, it becomes disabled but no animation occurs. Upon further investigation, I found that the problem results from the use of setState while the animation is active. For example, if you setState while the animation is running, the animation will just stop. However, I don't understand why setting state before animation begins doesn't work either. I'm using Animated.View and this is my code:

import React, { useState } from 'react'
import { Animated, View, Button } from 'react-native';

export default function App() {
  const [dis,setDis] = useState(false)
  const animatedMargin = new Animated.Value(0);
  const slideAnimation = () => {
    Animated.timing(animatedMargin, { //go right
      toValue: 200,
      duration: 1000,
      useNativeDriver: false 
    }).start()
    setTimeout(() => {
      Animated.timing(animatedMargin, { //return left
        toValue: 0,
        duration: 1000,
        useNativeDriver: false 
      }).start()
    }, 1000);
    setTimeout(() => { setDis(false) }, 2000); //enable button
  }
  return (
    <View style={{ marginTop:50 }}>
      <Animated.View style={{ left: animatedMargin }}>
        <View style={{ backgroundColor:'#00f', height:50, width:50 }}/>
      </Animated.View>
        <Button title='go' disabled={dis}
          onPress={()=>{
            setDis(true),
            slideAnimation()
          }}
        ></Button>
    </View>
  );
}


If I don't use parts of code for disabling and enabling buttons, the animation works fine.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Seems that problem is animated value here. Just wrap it by useRef like so:

const animatedMargin = React.useRef(new Animated.Value(0)).current;

For example, if your animated value changes from 0 to 1 in 500 ms, when you change state (re-render component) a new instance will be created and lose the current progress, that's why you need to wrap it by the useRef hook to create an instance only once.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!