-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: useContext in Pencil's todo list #16
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
context 通常會習慣獨立一個檔案
在這個 case,可以把 TodoList 中的 functions 和 state 都放到 context 這檔案中
|
||
function Layout() { | ||
const [currentPath, setCurrentPath] = useState(''); | ||
const pathArr = useLocation().pathname.split('/senpai/'); | ||
const path = pathArr[pathArr.length - 1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useMatch('senpai/:type')
return ( | ||
<div className="min-h-screen w-full bg-stone-800"> | ||
<h1 className="text-white text-2xl">Pencil's React workshop dashboard</h1> | ||
<nav className="flex justify-around box-border bg-cyan-700"> | ||
<Link |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id={ADD_TODO} | ||
name={ADD_TODO} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這兩行有用途嗎?
const [inputValue, setInputValue] = useState(item.text); | ||
const { updateTodo, toggleDataState, deleteItem } = useMethods(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這兩行怎麼不在 EditArea
和 ButtonArea
呼叫就好?
function EditArea({ item, methods, inputValue }) { | ||
return ( | ||
<div className={clsx('flex w-0 grow')}> | ||
{!item.isEdit && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這可以跟下面的合併
{item.isDone ? ( | ||
<p className={clsx('w-20 rounded-md', 'bg-stone-800 text-lime-300')}>Done</p> | ||
) : item.isEdit ? ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這好複雜
但其實 isDone 跟 isEdit 並沒有交疊關係不是嗎?
{isEdit ? <.../> : <.../>}
{isDone ? <.../> : <.../> }
import { useState } from 'react'; | ||
import Button from './Button'; | ||
|
||
function TodoForm({ item, updateTodo, toggleDataState, deleteItem }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這隻檔案沒用到吧
障眼法?
); | ||
} | ||
|
||
export const useMethods = () => useContext(MethodContext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
全部東西都放在同一個檔案裡
context
custom hook
component
很複雜
終於....有勞了...
真相欸