Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda