reactjs - NextJS 中的功能组件与 ES6 类
问题描述
NextJs文档和其他参考资料中提供的所有示例和教程都带有功能组件。
那么,上课呢?getInitialProps
当功能组件替换为 ES6 类时(或文档中提到的其他特性)会发生什么。替换后该页面仍然是 NextJS 页面吗?
解决方案
您可以使用类组件(就像我通常做的那样),只需确保将 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>
);
}
}
推荐阅读
- reactjs - React DnD Expected to find a valid target
- android - Not able to stop listening to stream in flutter
- mongodb - how to query mongo profile greater than a specific date
- javascript - 无法在 then 方法中调用函数
- excel - VBA Excel:如何将类放入数组中
- arrays - Reactjs:如何检查数组中的数据是否存在?
- c++ - 带有 std::async 的函数在 Linux 上不使用多个内核
- swift - 如何让用户快速在 xib 单元格内的文本字段中输入文本
- swift - 将其转换为所需的 Date.Function 返回 nil 值后获取 nil 值
- html - 单选按钮组的 HTML 5 自定义验证不起作用