因為實務上的需要來熟悉 React,就趁機筆記一下從 Vue 3 轉 React Hooks 的一些細節差異。這邊筆記下 useCallback 和 TS 搭配使用時的注意事項。

內容
1. 若你的 styled.<DOM> 要使用上層傳進來 props 的內容,那該值請在 function component 設定好後,再傳給 styled.<DOM> 內,以免 TS 報錯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
interface SwitchModeBtnProps { currentTheme: ThemeModeType; setCurrentTheme: (value: ThemeModeType) => void; } const SwitchModeBtn = ({currentTheme, setCurrentTheme}: SwitchModeBtnProps) => { const backgroundColor = currentTheme === ThemeMode.DARK ? "green" : "red" const StyledSwitchModeBtn = styled.button` background-color: ${backgroundColor}; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; ` return ( <StyledSwitchModeBtn onClick={()=> setCurrentTheme(currentTheme === ThemeMode.DARK ? ThemeMode.LIGHT : ThemeMode.DARK)}>切換模式</StyledSwitchModeBtn> ) } |
程式碼
https://github.com/andy922200/practice-20230615/commit/1e8640e09f16d6648d084b320c8488c1a5f9b4db
系列文章
[筆記] React Hooks 起來 – Ch7 – 切換畫面元件
[筆記] React Hooks 起來 – Ch6 – 建立自己的 Hook
[筆記] React Hooks 起來 – Ch6 – useMemo 的使用
[筆記] React Hooks 起來 – Ch5 – useState 和 useEffect
[筆記] React Hooks 起來 – Ch4 – 實作深色主題
[筆記] React Hooks 起來 – Ch4 – 增加狀態管理
[筆記] React Hooks 起來 – Ch4 – 前導
[筆記] React Hooks 起來 – Ch4 – SVG
[筆記] React Hooks 起來 – Ch3
[筆記] React Hooks 起來 – Ch1 ~ Ch2
按讚加入粉絲團
延伸閱讀