state-jet 2.0.17 → 2.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +33 -27
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -8,9 +8,9 @@ For more details, see [here](https://statejet.netlify.app).
8
8
 
9
9
  ## Table of Contents
10
10
 
11
- - [🚀 Why state-jet?](#🚀-why-state-jet?)
11
+ - [🚀 Why state-jet?](#why-state-jet?)
12
12
  - [Documentation](#documentation)
13
- - [🛠 Installation](#🛠-installation)
13
+ - [🛠 Installation](#installation)
14
14
  - [GlobalState](#globalstate)
15
15
  - [Create GlobalState](#create-globalstate)
16
16
  - [Binding Global State to a Component](#binding-global-state-to-a-component)
@@ -18,7 +18,7 @@ For more details, see [here](https://statejet.netlify.app).
18
18
  - [Create Slice](#create-slice)
19
19
  - [Store](#store)
20
20
  - [Create Store](#create-store)
21
- - [Binding Global State to a Component](#binding-global-state-to-a-component)
21
+ - [Binding Store to a Component](#binding-store-to-a-component)
22
22
  - [Middlewares](#middlewares)
23
23
  - [Logger Middleware](#logger-middleware)
24
24
  - [Reducer Middleware](#reducer-middleware)
@@ -29,7 +29,7 @@ For more details, see [here](https://statejet.netlify.app).
29
29
  - [Why state-jet Is More Advanced Than Zustand](#why-state-jet-is-more-advanced-than-zustand)
30
30
  - [FAQ](#faq)
31
31
  - [Conclusion](#conclusion)
32
- - [⚡ Comparison Table](#⚡-comparison-table)
32
+ - [⚡ Comparison Table](#comparison-table)
33
33
  - [Comparison with other libraries](#comparison-with-other-libraries)
34
34
  - [Contributing](#contributing)
35
35
  - [Publishing](#publishing)
@@ -47,9 +47,9 @@ For more details, see [here](https://statejet.netlify.app).
47
47
 
48
48
  Documentation: https://statejet.netlify.app/docs
49
49
 
50
- Tutorials: https://statejet.netlify.app/docs/category/tutorial
50
+ Tutorials: https://statejet.netlify.app/docs/tutorial/intro/
51
51
 
52
- API Reference: https://statejet.netlify.app/docs/category/api-reference
52
+ API Reference: https://statejet.netlify.app/docs/api-reference/global-state/
53
53
 
54
54
  Wiki: https://deepwiki.com/venkateshsundaram/state-jet
55
55
 
@@ -77,7 +77,7 @@ Or if you're using `cdn`:
77
77
 
78
78
  ## GlobalState
79
79
 
80
- The `useStateGlobal` hook is the simplest entry point to State-Jet. It allows you to create stateful values that can be accessed and updated from any component in your application, regardless of their location in the component tree.
80
+ The `useStateGlobal` hook is the simplest entry point to State-Jet—-ideal for simple applications with minimal state management needs. It allows you to create stateful values that can be accessed and updated from any component in your application, regardless of their location in the component tree.
81
81
 
82
82
  ### Create GlobalState
83
83
 
@@ -113,8 +113,6 @@ export default function Counter() {
113
113
 
114
114
  Slices in state-jet represent logical groupings of state that help organize application data into manageable pieces. Unlike the global state approach which uses a single namespace, slices allow for partitioning state into named segments, making state management more modular and maintainable.
115
115
 
116
- Each slice can contain multiple state values, each identified by a unique key within that slice.
117
-
118
116
  ### Create Slice
119
117
 
120
118
  ```ts
@@ -125,13 +123,13 @@ import { useSlice } from "state-jet";
125
123
  const productSlice = useSlice("products");
126
124
  const cartSlice = useSlice("cart");
127
125
 
128
- export const useProductSlice = () => productSlice("list", []);
129
- export const useCartSlice = () => cartSlice("list", []);
126
+ export const useProductSlice = () => productSlice("productState", {});
127
+ export const useCartSlice = () => cartSlice("cartState", {});
130
128
  ```
131
129
 
132
130
  ## Store
133
131
 
134
- The `useStore` hook serves as a mechanism to group related slices of state into a cohesive store, enabling modular and organized state management in React applications. It creates a persistent reference to a collection of slice instances that can be accessed throughout an application component tree.
132
+ The `useStore` hook serves as a mechanism to group related slices of state into a cohesive store, enabling modular and organized state management in React applications which are better suited for larger applications with more complex and structured state requirements.
135
133
 
136
134
  ### Create Store
137
135
 
@@ -152,7 +150,7 @@ const initializer: any = () => ({
152
150
  export const useEcommerceStore = () => useStore(initializer);
153
151
  ```
154
152
 
155
- ### Binding Global State to a Component
153
+ ### Binding Store to a Component
156
154
 
157
155
  ```tsx
158
156
  // file: src/components/ProductList.tsx
@@ -174,19 +172,27 @@ export const ProductList = () => {
174
172
  const store = useEcommerceStore();
175
173
  const products: any = store.products;
176
174
  const cart: any = store.cart;
177
- const productItems = products.useState() as ProductType[];
178
- const cartItems = cart.useState() as CartType[];
175
+ const productSliceData: any = products.useState();
176
+ const cartSliceData: any = cart.useState();
177
+ const productItems: Array<ProductType> = productSliceData?.items || [];
178
+ const cartItems: Array<CartType> = cartSliceData?.items || [];
179
179
 
180
180
  const addToCart = (product: ProductType) => {
181
181
  if (cartItems.some((cartItem: CartType) => cartItem.name === product.name)) {
182
- cart.set(cartItems.map((cartItem: CartType) => {
183
- if (cartItem.name === product.name) {
184
- return { ...cartItem, count: (cartItem.count || 0) + 1 };
185
- }
186
- return cartItem;
182
+ cart.set((cartVal: any)=> ({
183
+ ...cartVal,
184
+ items: cartItems.map((cartItem: CartType) => {
185
+ if (cartItem.name === product.name) {
186
+ return { ...cartItem, count: (cartItem.count || 0) + 1 };
187
+ }
188
+ return cartItem;
189
+ })
187
190
  }));
188
191
  } else {
189
- cart.set([...cartItems, { ...product, count: 1 }]);
192
+ cart.set((cartVal: any)=> ({
193
+ ...cartVal,
194
+ items: [...cartItems, { ...product, count: 1 }]
195
+ }));
190
196
  }
191
197
  };
192
198
 
@@ -194,7 +200,7 @@ export const ProductList = () => {
194
200
  <div>
195
201
  <h2>🛍️ Products</h2>
196
202
  <ul>
197
- {productItems.map((productItem: ProductType, index: number) => (
203
+ {productItems && productItems.map((productItem: ProductType, index: number) => (
198
204
  <li key={index}>
199
205
  {productItem.name} - ${productItem.price}{" "}
200
206
  <button onClick={() => addToCart(productItem)}>Add to Cart</button>
@@ -267,7 +273,7 @@ export default function Counter() {
267
273
 
268
274
  ### Reducer Middleware
269
275
 
270
- Can't live without reducer?. No worries. StateJet supports reducer middleware
276
+ Can't live without reducer? No worries, StateJet supports reducer middleware.
271
277
 
272
278
  ```ts
273
279
  // file: src/store/middleware.ts
@@ -329,7 +335,7 @@ export default function Counter() {
329
335
 
330
336
  ### Debounce Middleware
331
337
 
332
- You can delay the update of global state
338
+ You can delay the update of global state.
333
339
 
334
340
  ```ts
335
341
  // file: src/store/middleware.ts
@@ -363,7 +369,7 @@ export const counterState = useStateGlobal("counter", 0, { middleware: [debounce
363
369
 
364
370
  ### Optimistic Middleware
365
371
 
366
- You can optimistically update global state with rollback support
372
+ You can optimistically update global state with rollback support.
367
373
 
368
374
  ```ts
369
375
  // file: src/store/middleware.ts
@@ -401,7 +407,7 @@ export const profileState = useStateGlobal("profile", { name: "John" }, {
401
407
 
402
408
  ### Custom Middleware
403
409
 
404
- You can also create your own custom middleware in state-jet
410
+ You can also create your own custom middleware in state-jet.
405
411
 
406
412
  ```ts
407
413
  // file: src/store/middleware.ts
@@ -508,7 +514,7 @@ Development of State-jet happens in the open on GitHub, and we are grateful to t
508
514
  - [Contributing Guide](./CONTRIBUTING.md)
509
515
 
510
516
  ## Publishing
511
- - Before pushing your changes to Github, make sure that `version` in `package.json` is changed to newest version. Then run `npm install` for synchronize it to `package-lock.json` and `pnpm install` for synchronize it to `pnpm-lock.yaml`
517
+ Before pushing your changes to Github, make sure that `version` in `package.json` is changed to newest version. Then run `npm install` for synchronize it to `package-lock.json` and `pnpm install` for synchronize it to `pnpm-lock.yaml`
512
518
 
513
519
  ## Feedbacks and Issues
514
520
  Feel free to open issues if you found any feedback or issues on `state-jet`. And feel free if you want to contribute too! 😄
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-jet",
3
- "version": "2.0.17",
3
+ "version": "2.0.18",
4
4
  "description": "Ultra-lightweight global state management for React",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",