动态表单

初始化必须给{}赋值表单的name与值,否则会报不受控(即使是遍历出来的表单)

动态样式

方案一:CSS Module + classnames

  import classNames from 'classnames'
  function Render () {  
    return (
        <div className={
          classNames(
            ProgressStyles.fragment, {
              [ProgressStyles.fragmentActive]: idx <= activeIndex
            }
          )
        } key={idx}>{idx}</div>
    )
}

Notes: CSS Modules不支持连字符、下划线衔接。

.fragment {
  background: rgba(0, 0, 0, 0.06);
  flex: 1;

  & + & {
    margin: {
      left: 10px;
    };
  }

  &Active {
    background: #0F68FF;
  }
}

方案二:样式渗透

:global

  :global {
    .ant-select-selector {
      border: 1px solid #E5E5E5;
      background: linear-gradient(125.85deg, rgba(59, 63, 238, 0.1) 0%, rgba(57, 159, 254, 0.1) 100%);
      @media screen and (min-width: 550px) {
        height: nth($lineHeight, 1)!important;
        line-height: nth($lineHeight, 1)!important;
      }
      @media screen and (max-width: 550px) {
        height: nth($lineHeight, 2)!important;
        line-height: nth($lineHeight, 2)!important;
      }
    }
    .ant-select-selection-placeholder {
      @include placeholder;
    }
  }
  :global(.swiper-slide) {
    cursor: pointer;
  }

组件定义

Slot定义

SwiperCommon组件的调用,需要用SwiperCommon内部遍历的列表项数据:

export default function Render () {
  return (
    <SwiperCommon className="newsSwiper" metas={data.list} options={}>
        {
          (meta) => (
            <section className={NewsStyles.swiperSlide} onClick={() => handleClick(meta.id)}>
              <div className={NewsStyles.coverWrapper}>
                <Image alt={meta.title} src={meta.cover} className={NewsStyles.img}/>
              </div>
              <p className={NewsStyles.slideTitle}>{meta.title}</p>
              <p className={NewsStyles.summary}>{meta.summary}</p>
              <p className={NewsStyles.date}>{meta.time}</p>
            </section>
          )
        }
      </SwiperCommon>
  )
}

ScopedSlot定义:

import { useEffect } from 'react';
import SwiperCommonStyles from '@/styles/swiper-common.module.scss'
function SwiperCommon ({ children, metas = [], className: externalClass, options = {}}) {
  useEffect(() => {
    const swiper = new Swiper(`.${externalClass || 'swiper'}`, {
      direction: 'horizontal',
      centerInsufficientSlides: true,
      ...options
    });
  }, [externalClass, options])
  return (
    <>
      {
        metas.length && (
          <div className={classNames(SwiperCommonStyles.swiper, `swiper ${externalClass}`)}>
            <div className={classNames(SwiperCommonStyles.swiperWrapper, 'swiper-wrapper')}>
              {
                metas.map((item, idx) => (
                  <div key={idx} className={classNames(SwiperCommonStyles.swiperSlide, 'swiper-slide')}>
                    {children && children(item)}
                  </div>
                ))
              }
            </div>
          </div>
        )
      }
    </>
  )
}
export default SwiperCommon

参数解构

  <!--闭合标签使用时,组件参数类型声明中包含children-->
  <NavigationBar
    title="This is title"
  >
  </NavigationBar>
  <!--空标签使用时,组件参数类型声明中不包含children-->
  <NavigationBar
    title="This is title"
  />

组件的参数解构:

import NavigationBarStyles from "../styles/navigationbar.module.scss"
function Navigationbar ({ children, title = "", historyBack = false }) {
  return (
    <div className={NavigationBarStyles.container}>
      {historyBack && (<div className={NavigationBarStyles.historyBack}>&lt;</div>)}
      <NavigationbarContent title={title}>{children}</NavigationbarContent>
    </div>
  )
}
export default Navigationbar

Notes:

  • <NavigationBar />形式调用组件,解构出来的childrenundefined
  • <NavigationBar></NavigationBar> 形式调用组件,即使子节点为空,也能解构出来的children[]

全局SCSS变量配置

https://stackoverflow.com/questions/60951575/next-js-using-sass-variables-from-global-scss

const path = require('path');
const nextConfig = (phase, { defaultConfig }) => {
  if ('sassOptions' in defaultConfig) {
    defaultConfig['sassOptions'] = {
      includePaths: [path.join(__dirname, 'styles')],
      prependData: `@import "utils.scss";`,
    };
  }
  return defaultConfig;
};
module.exports = nextConfig;

全局可访问变量

在根目录下定义.env.env.development.env.production.env.local文件

Node环境

HOSTNAME=localhost
PORT=8080
HOST=http://$HOSTNAME:$PORT

Notes:

  • 以上变量在Node环境中可通过process.env.HOSTNAME形式获取,无法在浏览器中获取;
  • 可以通过$Variable的形式,在另一个变量定义中引用其他变量

Browser环境

NEXT_PUBLIC_API_BASEURL=http://127.0.0.1:3000

Notes:

  • 以上变量可以在Node、Browser环境中可通过process.env.NEXT_PUBLIC_API_BASEURL形式获取
  • NEXT_PUBLIC_ 前缀定义变量可以暴露在浏览器环境;

限制

  • 只能通过process.env.VarName的形式访问,无法通过解构、[变量]形式访问

    • process.env[varName] ——> Error
    • const env = process.env ——> Error

类型声明

https://dmitripavlutin.com/typescript-react-components/

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts

图片渲染

Nextjs图片渲染官方文档

  • 使用'next/image'提供的组件,会提供图片优化 — 懒加载
  • 使用'next/image'提供的组件,必须设定图片尺寸,图片尺寸若要根据父级尺寸,需设置父元素position: relative<Image layout="fill"... — 此时生成的img标签是绝对定位的。
import Image from 'next/image';
import welcomeStyles from '../styles/welcome.module.scss';

export default function Home() {
  return (
    <>
      <section className={welcomeStyles.navigationbar}>
        <h1 className='navigationbar-title'>new world</h1>
      </section>
      <section className={welcomeStyles.main}>
        <h2 className={welcomeStyles.title}>Welcome new world!</h2> 
        <div className={welcomeStyles.cover}>
          <Image layout="fill" objectFit="cover" src="/images/entries/wel.png" alt="" />
        </div>
      </section>
    </>
  );
}

客户端渲染

  useEffect(() => {
    import("../lib/flexible");
    import("../lib/flexibleHelper");
  }, []);

api Route

api定义

范指/pages/api/**/*.js路径下的文件,该处主要是转接服务端接口,中转前端数据结构。

//  /pages/api/blog
export default function handler(req, res) { // 导出的函数方法名无实际意义
  res.status(200).json({ name: 'John Doe' })
}

Notes: 该JavaScript是服务端代码,而非H5前端代码;

api访问

export async function getStaticProps(context) {
  const res = await fetch(`${process.env.BaseUrl}/api/blog`)
  const features = await res.json()
  return {
    props: {
      features,
    },
  }
}
export default function Blog ({features}) {
  const features = response
  return (
    <>
      <News meta={features.NewsResponse} />
    </>
  )
}

Notes: 前端必须通过http(s)协议请求,所以,必须启动服务才可以访问Api Route

导出纯静态页面

导出不需要服务配置,可独立访问的HTML静态页面(注意:SSG也是需要服务端配置的)
  1. 示例中的Nextjs在13.3(不含)以下版本:13.2.4
  2. 页面内的数据都是本地Mock的JSON数据
  3. 修改next.config.js配置:
const nextConfig = (phase, {defaultConfig}) => {
  defaultConfig.reactStrictMode = true
  defaultConfig.exportPathMap = async function (defaultPathMap) {
    return {
      '/': { page: '/' },
      '/news': { page: '/news' },
      '/blog': { page: '/blog' },
    }
  }
  defaultConfig.images.unoptimized = true
  return defaultConfig
}

module.exports = nextConfig
  • 配置exportPathMap字段,官网文档声明13.3版本以上会自动生成,无需配置
  • 配置images.unoptimized = true,不允许优化图片,否则会报错
  1. 补充package#scripts,运行npm run export
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "export": "next build && next export && npm run build-static-repair-index && npm run build-favicon-repair-index && npm run build-logo-repair-index",
    "build-static-repair-index": "replace-in-files --string \"/_next/static\" --replacement \"/_next/static\" out/index.html",
    "build-favicon-repair-index": "replace-in-files --string \"/favicon.ico\" --replacement \"./favicon.ico\" out/*.html",
    "build-logo-repair-index": "replace-in-files --string \"/logo*.png\" --replacement \"./logo*.png\" out/*.html",
    "start": "next start",
    "lint": "next lint"
  },
  • 静态打包后的资源引用地址不对,目前没找到配置项可用,需借助于replace-in-files单独处理资源引用。
  1. /out中的静态资源部署即可

米花儿团儿
1.3k 声望75 粉丝