我目前有以下 React 查詢實(shí)作:
const { data, isSuccess, isLoading } = useQuery(['myQuery', myParams], async () => { return myAjaxCall(myParams); }, { cacheTime: 0 });
我將傳回的結(jié)果傳遞給自訂元件:
<Results foundRecords={data}/>
我正在將初始搜尋結(jié)果傳遞到我的父頁面元件中,以便搜尋引擎抓取工具能夠在初始頁面載入時(shí)看到結(jié)果(如果請(qǐng)求是在客戶端發(fā)出的- 即使用useQuery( )
)。
在這種情況下,將傳遞到元件中的初始搜尋值(透過NextJS 的getServerSideProps()
)與useQuery()
實(shí)作整合的最佳方法是什麼?
從我的頭頂來看,它看起來像:
export async function getServerSideProps(context){ ... return { initialData: .... } } export default function MyPage({initialData}){ const { data, isSuccess, isLoading } = useQuery(['myQuery', myParams], async () => { return myAjaxCall(myParams); }, { cacheTime: 0 }); return ( <Results foundRecords={initialData || data}/> ) }
為了取得 Google 抓取工具的結(jié)果,您需要使用標(biāo)題和說明中提供的元數(shù)據(jù),還需要在 Google 控制臺(tái)中提交您的網(wǎng)域名稱
export default function Mypage({user}) { return (My page //Open Graph meta tags... Home> >) } export async function getServerSideProps ({ req }) { const user ={...} return {props: {user: user}} }
文件建議將資料放入useQuery的initialData
中。然後,您可以繼續(xù)使用從 useQuery
傳回的 data
:
export async function getServerSideProps(context){ ... return { initialData: .... } } export default function MyPage({initialData}){ const { data, isSuccess, isLoading } = useQuery(['myQuery', myParams], async () => { return myAjaxCall(myParams); }, { cacheTime: 0, initialData }); return () }