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

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

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