Skip to content

Instantly share code, notes, and snippets.

View showlovezz's full-sized avatar

vita hsieh showlovezz

  • Taiwan
View GitHub Profile
@showlovezz
showlovezz / useEffect8.js
Created May 5, 2020 17:08
鼠年全馬鐵人挑戰 - React Hooks useEffect 8
import React, { useState, useEffect } from 'react'
export default () => {
const [count, setCount] = useState(0)
const userData = [
{
code: 200,
message: 'Success',
user: {
@showlovezz
showlovezz / useEffect7.js
Last active May 5, 2020 16:55
鼠年全馬鐵人挑戰 - React Hooks useEffect 7
import React, { useState, useEffect } from 'react';
export default () => {
const [count, setCount] = useState(0)
const userData = [
{
code: 200,
message: 'Success',
user: {
@showlovezz
showlovezz / useEffect6.js
Created May 5, 2020 16:52
鼠年全馬鐵人挑戰 - 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)
}
@showlovezz
showlovezz / useEffect5.js
Created May 5, 2020 16:36
鼠年全馬鐵人挑戰 - React Hooks useEffect 5
const App = () => {
const [count, setCount] = useState(0)
useEffect(() => {
// 副作用邏輯
document.title = `You clicked ${count} times`;
}, [count])
useEffect(() => {
console.log(count)
@showlovezz
showlovezz / useEffect4.js
Last active May 5, 2020 16:37
鼠年全馬鐵人挑戰 - React Hooks useEffect 4
const App = () => {
const [user, setUser] = useState(null)
useEffect(() => {
// 初始化,獲取數據,僅會執行一次
setUser(userData)
}, [])
useEffect(() => {
// 更新數據
@showlovezz
showlovezz / useEffect3.js
Created May 5, 2020 01:03
鼠年全馬鐵人挑戰 - React Hooks useEffect 3
useEffect(() => {
// 該 effect 僅在初次畫面渲染之後執行一次,相當於 componentDidMount
})
useEffect(() => {
// 該 effect 會在初次畫面渲染之後執行一次,同時當陣列內的 count 發生改變時也會執行,相當於 componentDidUpdate
}, [])
@showlovezz
showlovezz / useEffect2.js
Created May 5, 2020 00:51
鼠年全馬鐵人挑戰 - React Hooks useEffect 2
useEffect(() => {
// 副作用處理
return () => {
// 清理工作,類似於 componentWillUnmount
}
})
@showlovezz
showlovezz / useEffect1.js
Last active May 3, 2020 16:07
鼠年全馬鐵人挑戰 - React Hooks useEffect 1
import React, { useState, useEffect } from 'react'
const Example = (props) => {
const [count, setCount] = useState(0);
useEffect(() => {
// 更新頁面標題
document.title = `You clicked ${count} times`
})
return (
<div>
@showlovezz
showlovezz / useState2.js
Last active May 5, 2020 16:39
鼠年全馬鐵人挑戰 - React Hooks useState 2
import React, { useState } from 'react'
const Example = () => {
// useState 採用 callBack Function,必要 return 一個值
const [count, setCount] = useState(() => {
return 0
})
return (
<div>
@showlovezz
showlovezz / useState1.js
Last active May 5, 2020 16:39
鼠年全馬鐵人挑戰 - React Hooks useState 1
import React, { useState } from 'react'
const Example = () => {
// useState 採用 0 為初始值
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>