# 新建nextjs + Tailwind CSS + styled components + tailwind-styled-components项目
使用nextjs、Tailwind CSS 、styled components 、tailwind-styled-components 以及Airbnb Eslint初始化React项目
## 安装nextjs + Tailwind CSS
LanguageCopy code```npx create-next-app@latest
```
LanguageCopy code```What is your project named? my-app Would you like to use TypeScript? Yes Would you like to use ESLint? Yes Would you like to use Tailwind CSS? Yes Would you like to use src/` directory? No Would you like to use App Router? (recommended) No Would you like to customize the default import alias? No
```
这里我没有选择App Router,目前nextjs App Router是基于react server components,目前它还不支持`styled components
`
## 安装styled components + tailwind-styled-components
LanguageCopy code```npm install styled-components
```
LanguageCopy code```npm install -D tailwind-styled-components
```
## 配置styled components
需要在`_document.tsx
`中配置
LanguageCopy code```jsimport Document from 'next/document'; import { ServerStyleSheet } from 'styled-components'; export default class MyDocument extends Document { static async getInitialProps(ctx) { const sheet = new ServerStyleSheet(); const originalRenderPage = ctx.renderPage; try { ctx.renderPage = () => originalRenderPage({ // eslint-disable-next-line react/react-in-jsx-scope,react/jsx-props-no-spreading enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />), }); const initialProps = await Document.getInitialProps(ctx); return { ...initialProps, styles: ( // eslint-disable-next-line react/react-in-jsx-scope <> {initialProps.styles} {sheet.getStyleElement()} </> ), }; } finally { sheet.seal(); } } }
```
## 使用tailwind-styled-components
LanguageCopy code```import tw from "tailwind-styled-components" export default function Home() { return ( <ContainerTW> </ContainerTW> ) } const ContainerTW = tw.div` w-screen min-h-screen bg-slate-700 `
```
LanguageCopy code```jsimport { useState } from "react" import tw from "tailwind-styled-components" export default function Home() { const [active] = useState(true); return ( <ContainerTW active={active}> </ContainerTW> ) } const ContainerTW = tw.div` ${p => p.active ? 'bg-red-400' : 'bg-slate-700'} w-screen min-h-screen `
```
## 使用tailwind-styled-components继承styled components
LanguageCopy code```import { useState } from "react" import { styled } from "styled-components"; import tw from "tailwind-styled-components" export default function Home() { const [active] = useState(true); return ( <ContainerTW active={active}> </ContainerTW> ) } const ContainerSC = styled.div` width: 300px; `; const ContainerTW = tw(ContainerSC)` ${p => p.active ? 'bg-red-400' : 'bg-slate-700'} w-screen min-h-screen `
```
## 设置Airbnb eslint
LanguageCopy code```npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-import eslint-config-airbnb-typescript prettier eslint-config-prettier eslint-plugin-prettier
```
修改根目录下的`.eslintrc.js
`
LanguageCopy code```module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: './tsconfig.json', // 指定tsconfig.json的路径 }, extends: [ 'airbnb-typescript', 'plugin:@typescript-eslint/recommended', 'plugin:react/recommended', 'plugin:react-hooks/recommended', 'plugin:jsx-a11y/recommended', 'plugin:import/errors', 'plugin:import/warnings', 'plugin:import/typescript', 'prettier', 'plugin:prettier/recommended', ], plugins: ['react', '@typescript-eslint', 'react-hooks', 'jsx-a11y', 'import'], env: { browser: true, es2021: true, node: true, }, rules: { 'react/react-in-jsx-scope': 'off', 'jsx-a11y/anchor-is-valid': [ 'error', { components: ['Link'], specialLink: ['hrefLeft', 'hrefRight'], aspects: ['invalidHref', 'preferButton'], }, ], 'react/prop-types': 'off', 'react/jsx-props-no-spreading': 'off', 'jsx-a11y/no-autofocus': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/no-unused-vars': ['error'], '@typescript-eslint/no-var-requires': 'off', 'import/prefer-default-export': 'off', 'react-hooks/rules-of-hooks': 'error', 'react-hooks/exhaustive-deps': 'warn', 'prettier/prettier': ['error', {}, { usePrettierrc: true }], }, settings: { react: { version: 'detect', }, }, };
```
根目录下创建`.prettierrc
`
LanguageCopy code```js{ "semi": true, "trailingComma": "all", "singleQuote": true, "printWidth": 80, "tabWidth": 2 }
```