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

how to pass functions as a props in react js

import React from 'react';
import './App.css';
import NewComponent from './components/NewComponent';
// import NewFunctionalComponent from './components/NewFunctionalComponent';

class App extends React.Component {

  constructor(){
    super()
    this.state = {
      isClicked: false
    }
    console.log(this.state)
  }

  handleClick = () => {
    console.log('I have been clicked')
    this.setState({
      isClicked: true
    }, () => console.log(this.state.isClicked))

  }

  render() {
    return (
      <div className="App">
        <NewComponent clickMe={this.handleClick} />
        {/* <NewFunctionalComponent noClickMe={this.handleClick} /> */}
      </div>
    )
  }
}

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: switch statement js 
Javascript :: remix js 
Javascript :: discord interaction create not working 
Javascript :: datatable ajax reload 
Javascript :: react live chat widget 
Javascript :: props history 
Javascript :: nodejs vs python 
Javascript :: js 2d array includes 
Javascript :: props 
Javascript :: query mongodb - nodejs 
Javascript :: classlist toggle 
Javascript :: random string javascript 
Javascript :: javascript number 
Javascript :: function in js 
Javascript :: get week number of month from date moment 
Javascript :: es6 import 
Javascript :: reactjs events list 
Javascript :: what does the useReducer do in react 
Javascript :: react datetime mannual editing 
Javascript :: javascript get all hidden elements 
Javascript :: javascript last elements same class 
Javascript :: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms 
Javascript :: display only initials from full name reactjs 
Javascript :: mongoose wont update value in array 
Javascript :: use global variable in anonymous function 
Javascript :: get number of elements in hashmap javascript 
Javascript :: await zoomus.joinmeeting crashing app react native 
Javascript :: node silent print to themral 
Javascript :: return $this-response-withType("application/json")-withStringBody(json_encode($result)); 
Javascript :: conflict paypal api javascript with user agent Mozilla/5.0 Google 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =