# 新建nextjs + Tailwind CSS + styled components + tailwind-styled-components项目

使用nextjs、Tailwind CSS 、styled components 、tailwind-styled-components 以及Airbnb Eslint初始化React项目

## 安装nextjs + Tailwind CSS

Language
Copy code
``` npx create-next-app@latest ```

Language
Copy 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

Language
Copy code
``` npm install styled-components ```

Language
Copy code
``` npm install -D tailwind-styled-components ```

## 配置styled components

需要在`_document.tsx`中配置

Language
Copy code
```js import 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

Language
Copy 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 ` ```

Language
Copy code
```js import { 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

Language
Copy 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

Language
Copy 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`

Language
Copy 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`

Language
Copy code
```js { "semi": true, "trailingComma": "all", "singleQuote": true, "printWidth": 80, "tabWidth": 2 } ```