-
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
apply swr for list page #34
base: main
Are you sure you want to change the base?
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/duoctvd/duoctv-trainning/AoX6pJx7EjgnnkfcHHgoupR7D1kn [Deployment for 1ad7029 failed] |
Khi sử dụng bất kỳ biến nào em đều phải khai báo kiểu dữ liệu cho nó, còn là kiểu gì thì tùy mục đích sử dụng của em. Đối với cái này https://gyazo.com/fde8a3b7cc5c518f3715f1838ff84b69, em tính truyền vào url dạng gì thì em khai báo cho nó dạng đó, trường hợp này thì chắc là Đối với cái này https://gyazo.com/e2a8425a43026e422d4b5b146fe5d785, em đang sử dụng API thì phải. Khi đọc tài liệu hoặc tìm kiếm thì nhớ lưu ý tới ts. Như tài liệu của nextjs thì nó có hướng dẫn cho ts |
src/pages/admin/news/list.tsx
Outdated
|
||
function List() { | ||
// let newsList: News[] = []; | ||
const { data, error } = useSWR('/api/news/retrievenews', fetcher); |
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.
@duoctvd
Bỏ xử lý useSWR vào src/hooks/useNewListData.ts
Còn fetcher không cần sử dụng api, em gọi trực tiếp lên firebase lun
(Cái này viết ví dụ thôi nha, em phải thêm khai báo và điều chỉnh lại)
export function useNewListData() {
const { data, error } = useSWR("useNewListData", async () => {
return await retrieveNews(); // just sample
});
return {
newsList: data,
isError: !!error,
isLoading: !data && !error,
};
}
Nói chung phần sử dụng useSWR và trả về data sẽ ở trong src/hooks/useNewListData.ts
Khi sử dụng em chỉ việc gọi đơn giản vầy:
const { newsList, isError, isLoading} = useNewListData();
What I have done today
|
src/pages/hooks/useNewListData.ts
Outdated
import useSWR from 'swr'; | ||
import { RetrieveNews } from "../../firestore/news/retrieveNews"; | ||
|
||
export function useNewListData() { |
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.
@duoctvd
Thêm kiểu trả về cho function
interface UseNewListData {
xxxx
}
export function useNewListData(): UseNewListData {
import useSWR from 'swr'; | ||
import { RetrieveNewsById } from "../../firestore/news/retrieveNewsById"; | ||
|
||
export function useRetrieveNewsDataById(idNews:string) { |
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.
@duoctvd
Tương tự phía trên, thêm kiểu trả về cho function.
Đổi tên idNews => newsId
export function useRetrieveNewsDataById(idNews:string) { | ||
|
||
const { data, error } = useSWR("useRetrieveNewsDataById", async () => { | ||
return await RetrieveNewsById(idNews); |
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.
@duoctvd
Cách truyền id vào như vậy đúng rồi.
Thường thì mấy file template (.tsx) sẽ viết hoa chữ đầu còn function (.ts) thì viết chữ đầu bình thường nha e.
RetrieveNewsById => retrieveNewsById
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.
@duoctvd
Cách truyền id vào như vậy đúng rồi.
Thường thì mấy file template (.tsx) sẽ viết hoa chữ đầu còn function (.ts) thì viết chữ đầu bình thường nha e.
RetrieveNewsById => retrieveNewsById
@bit-thuynt: Em đang dùng thế này để truyền newsId qua hooks, em đang cảm thấy nó k đc truyền qua mỗi lần request (vì còn còn dùng trong getServerSideProps), nên lúc thì nó qua đc data lúc thì nó là undifie
https://gyazo.com/f594699833b6e16bec1a73ad07de83fd
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.
@duoctvd
Tại router nó là bất đồng bộ nên news id lấy từ router cũng sẽ bất đồng bộ.
Vấn đề này thì xem sẽ thêm check bên trong useSWR
https://swr.vercel.app/docs/conditional-fetching#conditional
Khi nào news id có giá trị thì mới gọi get.
const { data, error } = useSWR( newsId ? "useRetrieveNewsDataById" : null, async () => {
What I have done
Questionkhi em đọc tài liệu về cách dùng useSWR
Nhưng cách mình dùng với firebase
thì thật sự em vẫn chưa thật sử hiểu về useNewListData trong useSWR("useNewListData"..... Vdu
lúc mình gọi thì mình sẽ gọi useNewListData của vậy chị ? |
@duoctvd
=> fetcher Trong docs của nó có nói key là gì fetcher là gì.
Tất nhiên là |
src/hooks/useNewListData.ts
Outdated
isLoading: boolean; | ||
} | ||
|
||
export function useNewListData() { |
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.
@duoctvd
Thêm kiểu trả về ở function để khi code của mình viết nó trả về không đúng thì fix lại code.
export function useNewListData(): UseNewListData{
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.
@duoctvd
Thêm kiểu trả về ở function để khi code của mình viết nó trả về không đúng thì fix lại code.export function useNewListData(): UseNewListData{
@bit-thuynt: sorry chị! em nhớ hôm qua em thêm cho nó rồi! mà chắc em adjust code debug sao sao sao push lên nó lại bị xóa đi, để em thêm lại
src/hooks/useNewListData.ts
Outdated
newsList: data, | ||
isError: !!error, | ||
isLoading: !data && !error, | ||
} as UseNewListData; |
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.
@duoctvd
bỏ as
chổ này vì đã khai báo trả về ở function.
Nếu có lỗi thì sửa lại chổ return hoặc kiểu trả về để cho cả 2 khớp nhau.
src/hooks/useRetrieveNewsDataById.ts
Outdated
import useSWR from 'swr'; | ||
import { retrieveNewsById } from "../firestore/news/retrieveNewsById"; | ||
|
||
interface useRetrieveNewsDataById { |
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.
@duoctvd
Đối với interface thì viết hoa chữ đầu
src/hooks/useRetrieveNewsDataById.ts
Outdated
news: data, | ||
isError: !!error, | ||
isLoading: !data && !error, | ||
} as useRetrieveNewsDataById; |
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.
tương tự ở trên, bỏ as useRetrieveNewsDataById
What I have done
Problem
|
import { useState } from "react"; | ||
import { newsPagination } from "src/firestore/news/newsPagination"; | ||
|
||
// export default function NewsListTemplate({ newsList }: { newsList: News[] }) { |
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.
@duoctvd
Những xử lý sau vẫn nằm trong NewsListTemplate
Em chỉ cần move phần layout sang presenter thôi
import { newsPagination } from "src/firestore/news/newsPagination"; | ||
import { nextPage,prevPage } from "src/components/templates/admin/news/list/index"; | ||
|
||
export default function NewsListTemplate({ newsList }: { newsList: News[] }) { |
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.
NewsListTemplate => NewsListPresenter
const router = useRouter(); | ||
var [newsListResult, setNewsListResult] = useState(newsList); | ||
const [end, setEnd] = useState(false); | ||
const [start, setStart] = useState(true); |
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.
Mấy xử lý này là thuộc container (news/list/index.tsx)
src/pages/admin/news/list.tsx
Outdated
@@ -1,7 +1,7 @@ | |||
import React from "react"; | |||
import Head from "next/head"; | |||
import styled from "styled-components"; | |||
import NewsListTemplate from "src/components/templates/admin/news/list"; |
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.
Chổ này vẫn giữ import như cũ
import NewsListTemplate from "src/components/templates/admin/news/list";
What I have done today
https://swr.vercel.app/docs/getting-started
https://nextjs.org/docs/basic-features/data-fetching#swr
https://nextjs.org/docs/api-routes/introduction
Em đã applued swr cho list page (hình như nó có cơ chế phân trang, nên hiện tại em đang tạm thời remove phân trang theo cách cũ). Em hy vọng cách em đang làm là đúng
Problem
Khi làm typscript và nextjs em thường xuyên bị lỗi về không khai báo biến thế này
https://gyazo.com/fde8a3b7cc5c518f3715f1838ff84b69
https://gyazo.com/e2a8425a43026e422d4b5b146fe5d785
trước kia em bị mấy lần ở file khác, chị có hướng dẫn em, tạo view và chuyển qua view nó hết, nhưng giờ em lại bị tiếp ở file khác nữa!
src/pages/api/news/retrievenews.ts
src/pages/admin/news/list.tsx
nên em vẫn chưa thật sự biết cách fix nó là gì (nó hay thông báo Parameter 'req' implicitly has an 'any' type, nên em toàn tính dùng req: any)