Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hooks send data from child to parent

const { useState } = React;

function PageComponent() {
  const [count, setCount] = useState(0);
  const increment = () => {
    setCount(count + 1)
  }

  return (
    <div className="App">
      <ChildComponent onClick={increment} count={count} />         
      <h2>count {count}</h2>
      (count should be updated from child)
    </div>
  );
}

const ChildComponent = ({ onClick, count }) => {
  return (
    <button onClick={onClick}>
       Click me {count}
    </button>
  )
};

ReactDOM.render(<PageComponent />, document.getElementById("root"));
Comment

PREVIOUS NEXT
Code Example
Javascript :: Uncaught TypeError: document.getContext is not a function 
Javascript :: redux reducer 
Javascript :: JavaScript BLOCK ENTER 
Javascript :: try catch with for loop in javascript 
Javascript :: how to convert a number to a string in javascript 
Javascript :: cypress test only one file 
Javascript :: legend on click use default chartjs 
Javascript :: javascript remove last item 
Javascript :: browser tab switch event js 
Javascript :: how to get location javascript 
Javascript :: select name get option value jquery 
Javascript :: angular get class list for element 
Javascript :: js convert object to array 
Javascript :: Send Email sgMail 
Javascript :: importing svg into cra 
Javascript :: Discord.js Get A Bot To Join A Music Chanel 
Javascript :: mongoose get value 
Javascript :: merge 2 arrays jquery 
Javascript :: jquery slider value 
Javascript :: anonymous functions javascript 
Javascript :: chart js donut 
Javascript :: react setstate in another component 
Javascript :: range command in JS 
Javascript :: print string multiple times in javascript 
Javascript :: javascript first letter uppercase 
Javascript :: one component to another component in vuejs trigger function 
Javascript :: implement singleton javascript 
Javascript :: javascript convert minus to plus 
Javascript :: accordion reatjs 
Javascript :: function to count words in string 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =