Skip to content

Instantly share code, notes, and snippets.

@b-tekinli
Last active April 25, 2024 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-tekinli/0988e665ede58b2bf021ee5abf45c820 to your computer and use it in GitHub Desktop.
Save b-tekinli/0988e665ede58b2bf021ee5abf45c820 to your computer and use it in GitHub Desktop.
use callback
import React, { useState, useCallback } from 'react';
const ParentComponent = () => {
const [count, setCount] = useState(0);
const handleIncrement = useCallback(() => {
setCount((prev) => prev + 1);
}, []); // referansın değişmemesi için
return (
<>
<div>{count}</div>
<ChildComponent onIncrement={handleIncrement} />
</>
);
};
const ChildComponent = ({ onIncrement }) => (
<button onClick={onIncrement}>Artır</button>
);
export default ParentComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment