Last active
July 23, 2019 00:05
-
-
Save will-wow/b5c10bb1e0ea9db4aeaa2868774ff1d8 to your computer and use it in GitHub Desktop.
Sample code for a blog post about shallow-testing hooks
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
const HelloWorld = ({ onLoad, onExit }) => { | |
// Set up local state | |
const [name, setName] = React.useState(""); | |
// run onLoad on mount, and onExit when object unmounts. | |
React.useEffect(() => { | |
onLoad(); | |
return onExit; | |
}, []); | |
// Say hello every time the name changes. | |
React.useEffect(() => console.log(`hello ${name}`), [name]); | |
// Control an input with the name. | |
return <input value={name} onChange={event => setName(event.target.value)} />; | |
}; |
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
const [activeAuthor, setActiveAuthor] = useState(null); | |
const [posts, setPosts] = useState([]); | |
React.useEffect(() => { | |
fetchPosts(activeAuthor.id).then(setPosts); | |
}, [activeAuthor]); |
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
const [authors, setAuthors] = useState([]); | |
React.useEffect(() => { | |
fetchAuthors().then(setAuthors); | |
}, []); |
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
jest.spyOn(React, 'useEffect').mockImplementation(f => f()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment