Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda