Created
June 5, 2022 10:10
-
-
Save oney/62137bc49c5d58c1b8a93d9e17660c6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { withStable } from "react-with-stable"; | |
import { useEffect } from "react-better-effect"; | |
function App({ movie }) { | |
const [uppercase, setUppercase] = useState(false); | |
const format = useCallback((str) => { | |
return uppercase ? str.toUpperCase() : str; | |
}, [uppercase]); | |
useSyncTitle(format, movie); | |
useShowRating(format, movie); | |
return ( | |
<div> | |
<Movie format={format} movie={movie} /> | |
<Click format={format} /> | |
</div> | |
); | |
} | |
function useSyncTitle(format, movie) { | |
useEffect(() => { | |
document.title = format(movie.name); | |
}, [format, movie]); | |
} | |
function useShowRating(format, movie) { | |
useEffect(($) => { | |
(async () => { | |
const rating = await fetchRating(movie.id); | |
showToast($.format(`This movie's rating is ${rating}`)); | |
})(); | |
}, [movie], {format}); | |
} | |
function Movie({ format, movie }) { | |
return <h1>{format(movie.name)}</h1>; | |
} | |
const Like = withStable(["format"], function Like({ format }) { | |
return <button onClick={() => alert(format("Liked this movie"))}>Like</button>; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment