跳到正文
TakoAPI
返回技能列表

cart-management

由 konscious0beast 提供

Web & Frontend Development
325下载量由 clawskills.sh 提供
0浏览量
0 个赞登录后即可为这个技能点赞
—暂无评分
你的评分

简介

React cart state management: duplicate prevention, localStorage persistence, CartContext patterns.

描述

该技能的功能

Patterns for managing shopping cart state in React apps. Covers duplicate prevention, localStorage persistence, and CartContext design. Centralizes cart logic to avoid scattered state across components.

Centralizing cart logic in a context prevents the duplicate-state bugs that arise when multiple components each manage their own cart slice.

何时使用

  • Preventing duplicate products when user clicks Add to Cart twice
  • Persisting cart contents after page refresh
  • Syncing cart state across multiple tabs
  • Updating item quantities without creating duplicate entries
  • Centralizing cart logic away from individual product components

示例工作流

输入

User asks: How do I add a product to the cart without allowing duplicates, and make it persist on refresh?

智能体
  1. 1

    Create a CartContext with cartItems and productIdsInCart state

  2. 2

    Initialize cartItems from localStorage on mount

  3. 3

    Implement addToCart that checks productIdsInCart.includes(item.id) before adding

  4. 4

    On each add, update both state arrays and write to localStorage

  5. 5

    Provide the context via CartProvider wrapping the app

输出

A CartContext where duplicate adds are silently ignored, cart survives refresh, and all product components consume a single source of truth.

安装

clawhub install konscious0beast/cart-management

运行 CLI 命令,或把这段提示词粘贴到 Claude Code、Cursor、Codex 或你在用的任意编程智能体里。

链接