「5」next-shopping:Error组件、Loading组件、Toast

我们先来定义几个通用组件,Icons的话会用到react-icons

安装依赖:

shell 复制代码
pnpm i react-icons

Icons组件

此次Icons组件只需要导出密码框的👀,🛒,登录图标和无权限图标,新建components/share/Icons.js

js 复制代码
import { BsEyeSlash, BsEye, BsCart2, BsShieldExclamation } from 'react-icons/bs'
import { FiLogIn } from 'react-icons/fi'

const Icons = {
  Eye: BsEye,
  EyeSlash: BsEyeSlash,
  Cart: BsCart2,
  Login: FiLogIn,
  Exclamation: BsShieldExclamation,
}

export default Icons

DisplayError组件

新建components/share/DisplayError.js

js 复制代码
import Icons from './Icons'

export default function DisplayError({ errors }) {
  return (
    <div className="error-msg">
      {errors && <Icons.Exclamation />}
      <span>{errors && errors.message}</span>
    </div>
  )
}

Loding组件

新建components/share/Loading.js

js 复制代码
export default function Loading() {
  return (
    <div>
      <div className='bar bar1'></div>
      <div className='bar bar2'></div>
      <div className='bar bar3'></div>
      <div className='bar bar4'></div>
      <div className='bar bar5'></div>
      <div className='bar bar6'></div>
      <div className='bar bar7'></div>
      <div className='bar bar8'></div>
      <style jsx>{`
  
        .bar {
          width: 8px;
          height: 15px;
          background: #fff;
          display: inline-block;
          transform-origin: bottom center;
          border-top-right-radius: 20px;
          border-top-left-radius: 20px;
          /*   box-shadow:5px 10px 20px inset rgba(255,23,25.2); */
          animation: loader 1.2s linear infinite;
        }
        .bar1 {
          animation-delay: 0.1s;
        }
        .bar2 {
          animation-delay: 0.2s;
        }
        .bar3 {
          animation-delay: 0.3s;
        }
        .bar4 {
          animation-delay: 0.4s;
        }
        .bar5 {
          animation-delay: 0.5s;
        }
        .bar6 {
          animation-delay: 0.6s;
        }
        .bar7 {
          animation-delay: 0.7s;
        }
        .bar8 {
          animation-delay: 0.8s;
        }

        @keyframes loader {
          0% {
            transform: scaleY(0.1);
            background: ;
          }
          50% {
            transform: scaleY(1);
            background: #e3e2e9;
          }
          100% {
            transform: scaleY(0.1);
            background: transparent;
          }
        }
      `}</style>
    </div>
  );
}

把他们统一到处,新建components/index.js

js 复制代码
export { default as Icons } from './share/Icons'
export { default as Loading } from './share/Loading'
export { default as DisplayError } from './share/DisplayError'

Toast组件

我们需要用到react-hot-toast,安装依赖:

shell 复制代码
pnpm i react-hot-toast

然后在app/(main)/layout.js中引入

js 复制代码
'use client'
import { Toaster } from 'react-hot-toast'
import StoreProvider from '@/app/StoreProvider'

export default function Layout({ children }) {
  return (
    <StoreProvider>
      {children}
      <Toaster position="top-right" toastOptions={{ style: { fontSize: '1rem' } }} />
    </StoreProvider>
  )
}

app/(main)/page.js中测试:

js 复制代码
'use client'
import { toast } from 'react-hot-toast'
export default function Page() {
  const onHandleClick = () => {
    console.log('aaaa')
    toast.success('成功啦')
  }
  return (
    <div>
      <button onClick={onHandleClick}>点击我显示toast</button>
    </div>
  )
}

效果如下:

感谢阅读,下次再见!

代码地址:feat: 添加部分通用组件 · liyunfu1998/next-shopping@ae3dab2 (github.com)

相关推荐
阿伟来咯~6 分钟前
vue3+Nest.js项目 部署阿里云
开发语言·javascript·ecmascript
不想上班只想要钱1 小时前
vue3使用<el-date-picker分别设置开始时间和结束时间时,设置开始时间晚于当前时间,开始时间早于结束时间,结束时间晚于开始时间
前端·javascript
Li_Ning212 小时前
为什么 Vite 速度比 Webpack 快?
前端·webpack·node.js
2501_915373882 小时前
Electron 入门指南
前端·javascript·electron
同志327133 小时前
用HTML+CSS做了一个网易云音乐客户端首页
前端·css
小猪欧巴哟3 小时前
pnpm install 安装项目依赖遇到 illegal operation on a directory, symlink 问题
前端·vue.js
独角仙梦境3 小时前
🚀🚀🚀学习这个思路,你也能手撸自己的专属vip脚手架🚀🚀🚀
前端
CJWbiu3 小时前
Github Action + docker 实现自动化部署
前端·自动化运维
关山3 小时前
在TS中如何在子进程中动态实例化一个类
前端
吃瓜群众i3 小时前
兼容IE8浏览器的8个实用知识点
前端·javascript