如何在Next.js中使用TypeScript编写getStaticProps。

huangapple go评论57阅读模式
英文:

How to use getStaticProps in typescript in nextJs

问题

I keep getting undefined when i try to get all posts from my backend, what am i doing wrong?

import { AppContext } from '@/helpers/Helpers'
import axios from 'axios'
import { GetStaticProps } from 'next'
import React, { useContext } from 'react'
import Tweet from './Tweet'
import { TweetsContainer } from './Tweets.styled'

export interface IAppProps {
}

export default function Tweets(props: any, { allPosts }: any) {

  console.log(allPosts);

  return (
    <div>
        <TweetsContainer>
            <div>
           </div>
    </TweetsContainer>
    </div>
  );
}

export const getStaticProps: GetStaticProps = async () => {
   const res = await axios.get(`http://localhost:7000/api/tweets`)
   const data = res.data
   
   return {
     props: { allPosts: data }   
   }
}

Isn't this a right way of calling data from an api in next js with typescript?

英文:

I keep getting undefined when i try to get all posts from my backend, what am i doing wrong?

import { AppContext } from &#39;@/helpers/Helpers&#39;
import axios from &#39;axios&#39;
import { GetStaticProps} from &#39;next&#39;
import React, {useContext} from &#39;react&#39;
import Tweet from &#39;./Tweet&#39;
import { TweetsContainer } from &#39;./Tweets.styled&#39;


export interface IAppProps {
}

export default function Tweets(props: any, { allPosts }: any) {
  
  console.log(allPosts);
  
  return (
    &lt;div&gt;
        &lt;TweetsContainer&gt;
            &lt;div&gt;
           &lt;/div&gt;
    &lt;/TweetsContainer&gt;
    &lt;/div&gt;
  );
}

 export const getStaticProps: GetStaticProps = async () =&gt; {
   const res = await axios.get(`http://localhost:7000/api/tweets`)
   const data = res.data
   
   return {
     props: { allPosts: data }   
   }
 }

Isn't this a right way of calling data from an api in next js with typescript?

答案1

得分: 2

以下是您要翻译的内容:

您需要定义函数返回的数据类型请看下面的示例其中包含用户数据

type UserData = {
  name: string,
  email: string,
  mobile: string
}

type Props = {
  data: UserData;
};

export const getStaticProps: GetStaticProps<Props> = async () => {
  const data: UserData = /* 获取用户数据的逻辑 */;
  return { props: { data } };
};

const Users = (props: Props) => {
  const { data } = props
  return (
    <div>
      <h1>{data.name}</h1>
      <p>{data.email}</p>
    </div>
  );
};

export default Users;
英文:

You will require to define the type of data that will be returned by the function. Have a look on the below example with user data.

type UserData = {
  name: string,
  email: string,
  mobile: string
}

type Props = {
  data: UserData;
};

export const getStaticProps: GetStaticProps&lt;Props&gt; = async () =&gt; {
  const data: UserData = /* logic to fetch user data */;
  return { props: { data } };
};

const Users = (props: Props) =&gt; {
  const { data } = props
  return (
    &lt;div&gt;
      &lt;h1&gt;{data.name}&lt;/h1&gt;
      &lt;p&gt;{data.email}&lt;/p&gt;
    &lt;/div&gt;
  );
};

export default Users;

huangapple
  • 本文由 发表于 2023年4月10日 19:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定