Skip to content

Instantly share code, notes, and snippets.

@showlovezz
Last active May 5, 2020 16:55
Show Gist options
  • Save showlovezz/c1adf6227a839d516c4474f933ad5f15 to your computer and use it in GitHub Desktop.
Save showlovezz/c1adf6227a839d516c4474f933ad5f15 to your computer and use it in GitHub Desktop.
鼠年全馬鐵人挑戰 - React Hooks useEffect 7
import React, { useState, useEffect } from 'react';
export default () => {
const [count, setCount] = useState(0)
const userData = [
{
code: 200,
message: 'Success',
user: {
id: 1234,
name: 'vita',
age: 33,
gender: 'boy',
},
},
]
// 這樣子會變成,每次按下 button 時,console.log(userData) 就會每次執行,如果要改成只要初始渲染的話,加個空 [] 即可。
useEffect(() => {
console.log(userData);
})
return (
<section>
<h1>Hooks0 useState</h1>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</section>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment