Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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}
        />
      )}
    </>
  );
};
Comment

passing props in react functional components

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

const root = ReactDOM.createRoot(document.getElementById('root'));
const element = <Welcome name="Sara" />;
root.render(element);
Comment

Passing functions as Props in react

const Banner = props => {
  const name = props.name
  return (
    <div>
      <p>Hello {name}</p>
      <button onClick={props.clickHandler}>Click Me</button>
    </div>
  )
}

function App() {
  const showAlert = () => {
    alert("Welcome!")
  }
  return (
    <div>
      <Banner name="Ranjeet" clickHandler={showAlert} />
    </div>
  )
}

export default App
Comment

PREVIOUS NEXT
Code Example
Javascript :: select jquery display none 
Javascript :: how to make a discord bot send a message 
Javascript :: gif as animation react 
Javascript :: how to separate string elements in javascript 
Javascript :: javascript fetch 
Javascript :: moment duratuion from hours 
Javascript :: javascript check negative number 
Javascript :: navigation prompt javascript 
Javascript :: react progress circle 
Javascript :: object literal js 
Javascript :: Example Of LinkedList In JavaScript 
Javascript :: check contect type axios response 
Javascript :: postmark with nodejs 
Javascript :: react native add bottom tab and drawer menu 
Javascript :: fix slow loading images in react 
Javascript :: flatlist inside flatlist react native 
Javascript :: how can i do metaname csrf token attrcontent in vanilla javascrip 
Javascript :: how to highlight active screen react native 
Javascript :: iconify react 
Javascript :: scroll js 
Javascript :: check if user is streaming discord js 
Javascript :: add language switcher to react-admin 
Javascript :: how to push array 
Javascript :: filter in javascipt 
Javascript :: click on browser.find_element_by_xpath with href 
Javascript :: jquery slider get value on change 
Javascript :: async/await 
Javascript :: jsx else if statement 
Javascript :: jquery default value 
Javascript :: reverse array in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =