Skip to content

Instantly share code, notes, and snippets.

@Goloburda
Created February 21, 2020 14:45
Show Gist options
  • Save Goloburda/da5149f2ae0f898b95ac3c2ad7a84dd7 to your computer and use it in GitHub Desktop.
Save Goloburda/da5149f2ae0f898b95ac3c2ad7a84dd7 to your computer and use it in GitHub Desktop.
React useCallback
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