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

116
Vistas
React Native - Access properties of exported functional component

Here is my code for a custom functional component (./Animations/ModalViewMoveUp.js)

import React, { useRef, useEffect } from 'react';
import { Animated, Easing, Text, View } from 'react-native';
import { ExtendedExceptionData } from 'react-native/Libraries/LogBox/LogBox';

export const ModalViewMoveUp = (props) => {
  const moveAnim = useRef(new Animated.Value(900)).current  
  const animIn = Animated.timing(
    moveAnim,
    {
      toValue: 50,
      duration: 1700,
      easing: Easing.elastic(),
      useNativeDriver:false
    }
  )
  const animOut = Animated.timing(
    moveAnim,
    {
      toValue: 1000,
      duration: 1700,
      easing: Easing.elastic(),
      useNativeDriver:false
    }
  )
    
  React.useEffect(() => {
    animIn.start();
  }, [moveAnim])

  
  return (
    
    <Animated.View                 
      style={{
        ...props.style,
        marginTop: moveAnim,         
      }}
    >
      {props.children}
    </Animated.View>
  );
}

And in ./App.js

import {ModalIconViewMoveDown} from './Animations/ModalIconViewMoveDown'
...
<ModalViewMoveUp >
  {/* other elements */}
   <TouchableOpacity onPress={()=>???}><Text>CANCEL</Text></TouchableOpacity>

</ModalViewMoveUp >

I basically want a way to start the exit animation animOut.start() on when the cancel button is pressed, but i cant even get a ref of the ModalViewMoveUp element in the TouchableOpacity , let alone call a function from it. Even something like this would work for me

<ModalViewMoveUp shouldStop={this.state.shouldStop}>
  {/* other elements */}
   <TouchableOpacity onPress={()=>setState{{shouldStop:true}}}><Text>CANCEL</Text></TouchableOpacity>

</ModalViewMoveUp >

And then in /ModalViewMoveUp.js

 React.useEffect(() => {
   if(shouldStop){
     animOut.start();
   }
    //animIn.start() runs on render()
  }, [moveAnim])

But i know i cant just set a state in an exported component. Do i have to convert my functional component to a class? Any code examples would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What i ended up doing: pass a state bool when calling the component

<ModalViewMoveUp shouldOpen ={this.state.shouldOpen } style ={styles.modalViewUp}>
  {/* other elements */}
   <TouchableOpacity onPress={()=>setState{{shouldOpen:true }}}><Text>CANCEL</Text></TouchableOpacity>

</ModalViewMoveUp >

and in /ModalViewMoveUp.js

return(
<View>
    {props.shouldOpen == true &&
    <Animated.View                 
      style={{
        ...props.style,
        marginTop: moveAnim,         
      }}
    >
      {props.children}
    </Animated.View>
    }
    {props.shouldOpen == false &&
    <Animated.View                 
      style={{
        ...props.style,
        marginTop: moveAnim,         
      }}
    >
      {animOut.start()}
      {props.children}
    </Animated.View>
    }
    </View>
  );
)

It works exactly as i want it to. If anyone has any contraindications feel free to share !

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