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

114
Visualizações
ReactJS - How to implement a callback function from JSX

I am having a question about how to implement a callback function. In my case, I have a React app with this structure: App > Child > Button components

The problem is I do not know how to write a callback function from Button to Child

I would like to update a value in Child (e.g: inputFromButton) after clicking the button in Button Component. The handleClick() is triggered and a value will be sent to the Child component. Could someone help me to do this?

Here is my code:https://codesandbox.io/s/nifty-stonebraker-0950w8 The App component

import React from 'react';
import Child from './Child';
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: 'Data from App'
    }
  }

  handleCallback = (childData) => {
    this.setState({ data: childData })
  }

  render() {
    const { data } = this.state;
    return (
      <div>
        <Child dataFromApp={data} />
      </div>
    )
  }
}
export default App

The Child component

import React from 'react';
import { renderButton } from './Button';
class Child extends React.Component {
    state = {
        inputFromApp: "",
        inputFromButton: ""
    }
    componentDidMount() {
        this.setState({
            inputFromApp: this.props.dataFromApp
        })
    }
    render() {
        const renderButtonItem = renderButton(this.props);
        const inputFromApp = this.state.inputFromApp
        const inputFromButton= this.state.inputFromButton
        return (
            <div>
                <input value={inputFromApp}></input>
                <br></br>
                <input value={inputFromButton}></input>
                <div>{renderButtonItem}</div>
            </div>
        )
    }
}
export default Child

The Button component

import React from 'react';
export const renderButton = (props) => {
    const handleClick = () => {
        console.log('handleClick() props data from App: ' + props.dataFromApp)

    }
    return (
        <button onClick={handleClick}>Click</button>
    )
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. renderButton is a function component and, therefore, needs to be in PascalCase: RenderButton (although it would be better off as Button).

  2. Move handleClick to the Child component.

  3. Then in Button the call to handleClick should be props.handleClick since handleClick will now be a property of the props object passed into the component. We don't need to pass down the data as a prop to the button but can, instead just log the data prop passed into Child.

    handleClick = () => {
      console.log(`handleClick(): ${props.dataFromApp}`);
    }
    
  4. In Child, instead of calling renderButton, import Button, and then use that in the render passing down the handler in the props. By doing this you're making the component as "dumb" as possible so it can be reused elsewhere in the application.

    <Button handleClick={this.handleClick} />
    
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