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

161
Visualizações
How to pass function from class parent component to child functional component

What I have been trying is this:

The parent component is a class component with a function logToConsole. I summarized the code below:

I am trying to pass Function from parent component to a child functional component

Parent Component:

logToConsole = () => {
  console.log("test);
}
render(){
  return(
    <ChildComp logThis={this.logToConsole} />
  )
}

The ChildComp is:

const ChildComp = (props) => (
  <button onClick={()=>props.logThis()}>Click Here</button>
)

export default ChildComp

However I am getting logThis is not a function. How do I solve this issue is there a better way to do this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Parent:

import React from "react";
import Child from "./Child";

class Parent extends React.Component {
  logToConsole = () => {
    console.log("test");
  };
  render() {
    return <Child log={this.logToConsole} />;
  }
}
export default Parent;

Child:

export default function Child(props) {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button onClick={props.log}>Log</button>
    </div>
  );
}

Live Demo

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Relatório

0

Your child component should be:

<button onClick={props.logThis}>Click Here</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

logToConsole = () => {
  console.log("test);
}

should become

const logToConsole = () => {
    console.log("test");
};
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