Answers for "how to pass a function as prop in react?"

C
1

how to pass function as a props in react in functional components

function App() {
  const [status, setStatus] = React.useState(false);
  const [text, setText] = React.useState("");
  const handleClick = () => {
    this.setStatus(prev => ({ status: !prev.status }));
  };
  const handleChange = e => {
    this.setStatus({ text: e.target.value });
  };

  return (
    <>
      <button onClick={handleClick}>Open photo entry dialog</button>
      <ChildComponent
        isOpen={status}
        text={text}
        handleChange={handleChange}
        handleClick={handleClick}
      />
    </>
  );
}

const ChildComponent = ({ isOpen, text, handleChange, handleClick }) => {
  return (
    <>
      {isOpen && (
        <Model
          status={isOpen}
          handleClick={handleClick}
          text={text}
          handleChange={handleChange}
        />
      )}
    </>
  );
};
Posted by: Guest on December-23-2021
0

function component with props

import React from "react"

function Checkbox(props){
    return (
        <div>
            <input type="checkbox" />
            <label>{props.value}</label>
        </div>
    )
}

export default Checkbox
Posted by: Guest on April-13-2021

Code answers related to "how to pass a function as prop in react?"

Code answers related to "C"

Browse Popular Code Answers by Language