Created
May 5, 2020 16:52
-
-
Save showlovezz/7e9d480c1b477fe458714c098abe9882 to your computer and use it in GitHub Desktop.
鼠年全馬鐵人挑戰 - React Hooks useEffect 6
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, useEffect } from 'react' | |
export default () => { | |
const [user, setUser] = useState(null) | |
const fetchUser = async () => { | |
const result = await fetch('./user.json').then(res => res.json()) | |
setUser(result.user) | |
} | |
useEffect(() => { | |
fetchUser() | |
}, []) | |
return ( | |
<section> | |
<p>{user && user.name}</p> | |
<button onClick={() => setUser()}> | |
Click me | |
</button> | |
</section> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment