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

118
Views
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 answers
Answer question

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 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!