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

168
Views
Passing ref to class component from functional component

I have a parent functional component in my react-native app, and a child class component. I want to call a method of child in my parent component. I try to pass a ref to child and use it to call my method in parent.

my parent component:

const Screen = ({...props}) => {
    // ...
    let cats;

    const onHeadSearchPress = () => {
        console.log('check', cats); // log undefined
        cats.toggleCategoryList();
        cats.renderSearch();
    };    

    retrun (
        // ...
        <Categories onRef={ref => (cats = ref)} />
        // ...
    )
} 

my child component:

class Component extends React.PureComponent {
    constructor(props) {
    super(props);
    this.state = {
        //..
    };

    toggleCategoryList() {
        if (this.state.showList === false) {
            Animated.timing(this.animateChevronY, {
                toValue: 1,
                duration: 300,
                useNativeDriver: true,
                easing: Easing.linear,
            }).start();
        }
        if (this.state.showList === true) {
            Animated.timing(this.animateChevronY, {
                toValue: 0,
                duration: 300,
                useNativeDriver: true,
                easing: Easing.linear,
            }).start();
        }

        setTimeout(() => {
            this.setState({
                showList: !this.state.showList,
            });
        }, 100);
    }

    renderSearch() {
        this.setState({
            showSearch: true,
        });
        Animated.timing(this.searchBoxFade, {
            toValue: 1,
            duration: 200,
            easing: Easing.linear,
        }).start();
    }        

    componentDidMount() {
        console.log('checkthis', this); // log correctly
        this.props.onRef(this);
    }

    render() {
       // ...
    }
}

But log in parent make undefined and methods not running and make error "Cannot read property of undefined (reading toggleCategoryList)"

enter image description here

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

0

If I understand, you don't need a onRef event. You can use ref. And if your app is crashing with first render, there should be a null check.

   if(cats){
    cats.toggleCategoryList?.();
    cats.renderSearch?.();
   }
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!