reactjs - React Hook : Send data from child to parent component - Stack Overflow

PHOTO EMBED

Wed Oct 06 2021 21:25:25 GMT+0000 (Coordinated Universal Time)

Saved by @richard #javascript

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"));
content_copyCOPY

https://stackoverflow.com/questions/55726886/react-hook-send-data-from-child-to-parent-component