next.js 创建 react ant design ts 项目

环境说明:next.js 官方文档要求node版本在16.8以上。笔者使用的 node版本是16.20.1,不要使用16.13.0,笔者在使用 node16.13.0环境时创建的 react 项目点击事件无效

next.js官网截图

next.js 官网:https://nextjs.org/

react 官网:https://react.dev/

Ant Design 官网:https://ant.design/index-cn

目录

1、创建项目

[2、安装 ant design](#2、安装 ant design)

3、运行测试


1、创建项目

在电脑存放项目的文件夹下打开cmd窗口

执行命令创建项目 npx create-next-app antd-demo

bash 复制代码
npx create-next-app antd-demo

第一次创建会先安装 create-next-app

接下来会让你选择创建项目需要的内容,这里可以根据自己的喜好决定,或者直接使用默认

笔者选择的是使用 ts 和 app router

创建完成

2、安装 ant design

进入项目目录 ,使用 vscode编辑器打开项目

bash 复制代码
cd antd-demo
bash 复制代码
code ./

安装 ant design react

bash 复制代码
npm install antd --save
bash 复制代码
npm install @ant-design/cssinjs --save

安装完成后,在项目src目录下新建 lib 目录,在新建的 lib 目录下新建 AntdRegistry.tsx

AntdRegistry.tsx内容

TypeScript 复制代码
'use client';

import React from 'react';
import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs';
import { useServerInsertedHTML } from 'next/navigation';

const StyledComponentsRegistry = ({ children }: { children: React.ReactNode }) => {
  const cache = createCache();
  useServerInsertedHTML(() => (
    <style id="antd" dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }} />
  ));
  return <StyleProvider cache={cache}>{children}</StyleProvider>;
};

export default StyledComponentsRegistry;

vscode 截图

修改src/app/layout.tsx 内容为下面内容

TypeScript 复制代码
import React from 'react';
import { Inter } from 'next/font/google';
import StyledComponentsRegistry from '../lib/AntdRegistry';
import '@/app/globals.css';

const inter = Inter({ subsets: ['latin'] });

export const metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
};

const RootLayout = ({ children }: { children: React.ReactNode }) => (
  <html lang="en">
    <body className={inter.className}>
      <StyledComponentsRegistry>{children}</StyledComponentsRegistry>
    </body>
  </html>
);

export default RootLayout;

vscode 截图

添加主题配置

在项目根目录下新建 theme 文件夹,新建 themeConfig.ts

themeConfig.ts 内容

TypeScript 复制代码
// theme/themeConfig.ts
import type { ThemeConfig } from 'antd';

const theme: ThemeConfig = {
  token: {
    fontSize: 16,
    colorPrimary: '#52c41a',
  },
};

export default theme;

vscode 截图

修改app下page.tsx 内容为下面内容

TypeScript 复制代码
'use client';
import React from 'react';
import { Button, message, ConfigProvider } from 'antd';
import theme from './../../theme/themeConfig';

export default function Home() {
  const [messageApi, contextHolder] = message.useMessage();
  const add = ()=>{
    messageApi.open({
      type: 'success',
      content: '宜将剩勇追穷寇,不可沽名学霸王',
    });
  }
  return (
    <ConfigProvider theme={theme}>
      <div className="App">
        {contextHolder}
        <Button onClick={add} type="primary">Button</Button>
      </div>
    </ConfigProvider>
  )
}

vscode 截图

3、运行测试

在项目根目录下打开cmd,也可使用vscode自带的终端运行命令,npm run dev

bash 复制代码
npm run dev

浏览器访问:http://localhost:3000

这个背景的条纹是next.js自带的样式,如果想去掉它,可以将 app/globals.css 的 body 标签的背景background 样式去掉

css 复制代码
body {
  margin: 0;
  color: rgb(var(--foreground-rgb));
  /* background: linear-gradient(
      to bottom,
      transparent,
      rgb(var(--background-end-rgb))
    )
    rgb(var(--background-start-rgb)); */
}

运行效果

至此完

相关推荐
じòぴé南冸じょうげん2 小时前
若依框架favicon.ico缓存更新问题解决方案:本地生效但线上未更新
前端·javascript·前端框架·html
狮子座的男孩2 小时前
js基础高级:01、数据类型(typeof、instanceof、===的使用)、数据与变量与内存(定义、赋值与内存关系、引用变量赋值、js调函数传参)
前端·javascript·经验分享·数据类型·数据与变量与内存·赋值与内存关系·引用变量赋值
Cyclo-5 小时前
PDFJS 在React中的引入 使用组件打开文件流PDF
前端·react.js·pdf
椒盐螺丝钉7 小时前
Vue Router应用:组件跳转
前端·javascript·vue.js
顾安r7 小时前
11.20 开源APP
服务器·前端·javascript·python·css3
敲敲了个代码8 小时前
CSS 像素≠物理像素:0.5px 效果的核心密码是什么?
前端·javascript·css·学习·面试
少云清8 小时前
【软件测试】5_基础知识 _CSS
前端·css·tensorflow
倔强青铜三8 小时前
AI编程革命:React + shadcn/ui 将终结前端框架之战
前端·人工智能·ai编程
二川bro8 小时前
第57节:Three.js企业级应用架构
开发语言·javascript·架构
天外飞雨道沧桑9 小时前
前端开发 Cursor MCP 提效工具配置
前端·vscode·ai编程·开发工具·cursor