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

相关推荐
Eloudy1 小时前
ReAct 原理简介
前端·javascript·人工智能·react.js·agent·gpu
ZJU_统一阿萨姆2 小时前
【DSH】如何将 DeepSeek Harness 嵌入生产环境?万字解构 DeepSeek Agent Harness 的运行机制与安全边界
前端·javascript·安全
用户921080262862 小时前
4. 前端地图大量点位实时更新优化:后端推增量,前端做 diff
前端
嘟嘟07172 小时前
Next.js 博客左侧笔记列表是怎么从 Redis 渲染出来的?从组件、children 到动态路由一次讲清
redis·react.js·前端工程化
SoaringHeart2 小时前
Flutter最佳实践:Web项目中文字体首次加载乱码问题解决
前端·flutter
西安小哥3 小时前
破局与重生:大厂前端如何借力 AI 转型“超级全栈“
前端·人工智能
sunly_3 小时前
TypeScript总结:16、面向对象速查
前端·javascript·typescript
张元清4 小时前
React useInterval Hook:没有过期闭包的 setInterval (2026)
javascript·react.js
MXN_小南学前端4 小时前
React超长文本域中实现“返回顶部”浮动按钮
前端·javascript·react.js
学习嵌入式的小周4 小时前
Notepad++8.8.7下载安装(附安装包)
前端·notepad++