「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)

相关推荐
crary,记忆2 分钟前
Angular如何让整个项目的所有页面能够整体缩小一定的比例?
javascript·ecmascript·angular.js
Mintopia17 分钟前
🤖 算法偏见修正:WebAI模型的公平性优化技术
前端·javascript·aigc
Mintopia20 分钟前
🧩 TypeScript防御性编程:让Bug无处遁形的艺术
前端·typescript·函数式编程
JarvanMo22 分钟前
🔔 Flutter 本地通知: 吸引用户的完整指南—即使在他们离线时也能实现
前端
你想考研啊27 分钟前
一、redis安装(单机)和使用
前端·数据库·redis
江城开朗的豌豆29 分钟前
小程序与H5的“握手言和”:无缝嵌入与双向通信实战
前端·javascript·微信小程序
天蓝色的鱼鱼29 分钟前
React 19 发布一年后:对比 React 18,带来了哪些惊喜与变革
前端·react.js
你的电影很有趣33 分钟前
lesson73:Vue渐进式框架的进化之路——组合式API、选项式对比与响应式新范式
javascript·vue.js
江城开朗的豌豆34 分钟前
小程序静默更新?用户却无感?一招教你“强提醒”
前端·javascript·微信小程序
小张成长计划..35 分钟前
VUE工程化开发模式
前端·javascript·vue.js