javascript - Nextjs Fetch data when reloading the page - Stack Overflow

In React we fetch data using useEffect when reloading the page:useEffect(() => {let ignore = falseco

In React we fetch data using useEffect when reloading the page:

useEffect(() => {
  let ignore = false
  const fetchingData = async () => {
    const res = await fetch(<your_path>)
    const data = await res.json()
    if (!ignore) setData(data)
  }; 
  fetchingData()
  return () => { ignore = true }
}, [])

But how can I do this in Next.js? Fetch doesn't fire in getInitialProps when the page reloads.

In React we fetch data using useEffect when reloading the page:

useEffect(() => {
  let ignore = false
  const fetchingData = async () => {
    const res = await fetch(<your_path>)
    const data = await res.json()
    if (!ignore) setData(data)
  }; 
  fetchingData()
  return () => { ignore = true }
}, [])

But how can I do this in Next.js? Fetch doesn't fire in getInitialProps when the page reloads.

Share Improve this question edited Oct 13, 2019 at 19:50 Melchia 24.4k23 gold badges108 silver badges129 bronze badges asked Oct 13, 2019 at 19:35 ArthurArthur 3,5267 gold badges36 silver badges66 bronze badges 2
  • 1 Your question is my answer, thanks. – Fatemeh Qasemkhani Commented Apr 13, 2020 at 5:23
  • @FatemehQasemkhani, my question was based on my inpetence in next.js, later I realized that the problem with axios request in getInitialProps. Glad to help!:) – Arthur Commented Apr 13, 2020 at 8:51
Add a ment  | 

2 Answers 2

Reset to default 2

use axios or isomorphic-unfetch. they work both in client and server environments. Fetch API does not exist in the Node.js environment. it. If you still want to use Fetch API in your client-side code, you should use a isomorphic-unfetch. When you are in the browser, it uses the unfetch polyfill. If your code runs in Node.js, it switches to node-fetch.

import "axios" from "axios"

static async getInitialProps(){
    const res=await axios.get('url')//
    const data=res.data
    return {data}
}

UPDATE

In addition to fetch() on the client-side, Next.js polyfills fetch() in the Node.js environment. You can use fetch() in your server code (such as getStaticProps/getServerSideProps) without using polyfills such as isomorphic-unfetch or node-fetch.

https://nextjs/docs/basic-features/supported-browsers-features

In Next.js you usually load data with HTTP requests in getInitialProps then you can use them as props:

const App = props => (
  <Layout>
  {props.data}
    ...
  </Layout>
);

App.getInitialProps = async function() {
  const res = await fetch(<your_path>)
  const data = await res.json()
  return {
   data
  }
};

export default App;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745611926a4635996.html

相关推荐

  • javascript - Nextjs Fetch data when reloading the page - Stack Overflow

    In React we fetch data using useEffect when reloading the page:useEffect(() => {let ignore = falseco

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信