Pass down callback function

Report a typo

Props enable you to pass down callback functions from a parent component to a child component, enabling interaction and communication between components. Write a React functional component named Button that gets a callback function as a prop labelled onClickHandler. Create a button element that calls the provided onClickHandler function when clicked. Fill in the missing parts.

Fill in the gaps with the relevant elements
/* Button.jsx */
import React from 'react';

const Button = ({  }) => {
  return (
    <button ={onClickHandler}>
      Click me
    </button>
  );
};

export default Button;

/* ParentComponent.jsx */
import React from 'react';
import Button from './Button';

const ParentComponent = () => {
  const handleClick = () => {
    console.log('Button clicked!');
  };

  return (
    <div>
      < onClickHandler={} />
    </div>
  );
};

export default ParentComponent;

 

Create a free account to access the full topic