【问题标题】:Is there any way to use redux toolkit inside getStaticProps in Next.js?有没有办法在 Next.js 的 getStaticProps 中使用 redux 工具包?
【发布时间】:2021-11-19 13:54:08
【问题描述】:

当我使用useEffect 而不是getStaticProps 时,我得到了数据。但是在getStaticProps 中,它表明钩子只能在功能组件中使用。

import Head from 'next/head'
import Image from 'next/image'
import Sidebar from '../components/Sidebar'
import styles from "../styles/Home.module.css"
import CssBaseline from '@mui/material/CssBaseline'
import Navbar from '../components/Navbar'
import Mainbody from '../components/Mainbodi'
import { useGetCryptosQuery } from '../services/CryptoApi'

export default function Homee({res}) {
   console.log(res);
   return (
      <div>
         <div className={styles.container}>
            <CssBaseline />
            <Sidebar/>
            <div className={styles.bodi}>
               <Navbar/>
               <Mainbody/>
            </div>
         </div>
       </div>
   )
}

export const getStaticProps = async() => {
   const {data, isFetching} = await useGetCryptosQuery()
   return {
      props: {
         res: data.data.coins
      }
   }
}

【问题讨论】:

  • https://www.merixstudio.com/blog/introduction-using-redux-toolkit-nextjs-and-typescript/

标签: reactjs next.js server-side-rendering redux-toolkit


【解决方案1】:

目前,只有在非 SSR 场景中才能使用带有 Next 的 RTK Query。 RTK 版本 1.7 将改变这一点。

https://github.com/reduxjs/redux-toolkit/pull/1277

您可以通过 Next 随意使用 RTK 的其余部分。

【讨论】:

    猜你喜欢
    • 2022-07-20
    • 2021-03-17
    • 2021-03-16
    • 2021-10-21
    • 2022-01-25
    • 2022-01-03
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多