首页 > 解决方案 > NextJS 中的功能组件与 ES6 类

问题描述

NextJs文档和其他参考资料中提供的所有示例和教程都带有功能组件。

那么,上课呢?getInitialProps当功能组件替换为 ES6 类时(或文档中提到的其他特性)会发生什么。替换后该页面仍然是 NextJS 页面吗?

标签: reactjsnext.js

解决方案


您可以使用类组件(就像我通常做的那样),只需确保将 static 关键字添加到getInitialProps方法中:

import React from 'react';
import PropTypes from 'prop-types';

export default class Gallery extends React.Component {
    static propTypes = {
        image: PropTypes.string
    };

    static getInitialProps() {
        return {...};
    }

    render() {
        return (
            <div>
                // render gallery
            </div>
        );
    }
}


推荐阅读