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

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

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