gpt4 book ai didi

reactjs - next.js - getInitialProps 在组件中不起作用

转载 作者:行者123 更新时间:2023-12-01 21:46:03 24 4
gpt4 key购买 nike

这是我的代码

import React from 'react'
import fetch from 'isomorphic-unfetch'
import Book from './Book'

function getNum(val) {
val = +val || 0;
return val;
}

class BookList extends React.Component {
static async getInitialProps(ctx) {;
const res = await fetch('/api/books');
const json = await res.json();
return { books: json }
}

render() {
var books = this.props.books;

由于某些原因,render 函数中的“books”未定义。为什么 getInitialProps 在组件中不起作用?

最佳答案

getInitialProps 只能添加到页面导出的默认组件中,添加到任何其他组件都不起作用。

关于reactjs - next.js - getInitialProps 在组件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60555353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com