Created
February 21, 2020 14:45
-
-
Save Goloburda/da5149f2ae0f898b95ac3c2ad7a84dd7 to your computer and use it in GitHub Desktop.
React useCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import Child from "./Child"; | |
import "./styles.css"; | |
const MyReact = { | |
storage: {}, | |
myCallback: function(fn) { | |
if (this.storage.fn === undefined) { | |
this.storage.fn = fn; | |
} | |
return this.storage.fn; | |
} | |
}; | |
export default function App() { | |
const [count, setCount] = useState(0); | |
const sayHello = MyReact.myCallback(() => { | |
setCount(prev => (prev += 1)); | |
}); | |
return ( | |
<div className="App"> | |
{console.log("parent render")} | |
<h1>Hello {count}}</h1> | |
<Child sayHello={sayHello} /> | |
<h2>Start editing to see some magic happen!</h2> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment