Skip to content

Instantly share code, notes, and snippets.

@showlovezz
Created May 5, 2020 16:52
Show Gist options
  • Save showlovezz/7e9d480c1b477fe458714c098abe9882 to your computer and use it in GitHub Desktop.
Save showlovezz/7e9d480c1b477fe458714c098abe9882 to your computer and use it in GitHub Desktop.
鼠年全馬鐵人挑戰 - React Hooks useEffect 6
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