react-reactive-val 1.0.5 → 2.0.0
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.
- package/README.md +55 -19
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +15 -8
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# React Reactive Val
|
2
2
|
|
3
|
-
A lightweight, type-safe reactive value management library for React applications. This library provides a simple way to manage reactive values in React components with minimal boilerplate.
|
3
|
+
A lightweight, type-safe reactive value management library for React applications. This library provides a simple way to manage reactive values in React components with minimal boilerplate and includes powerful Context integration for efficient state sharing.
|
4
4
|
|
5
5
|
## Features
|
6
6
|
|
@@ -10,6 +10,7 @@ A lightweight, type-safe reactive value management library for React application
|
|
10
10
|
- 🔄 **Reactive**: Values update automatically across components
|
11
11
|
- 📦 **Tree-shakeable**: Only import what you need
|
12
12
|
- 🌳 **Multiple formats**: Supports ESM, CommonJS, and UMD
|
13
|
+
- 🌟 **Context Support**: Share reactive values efficiently through React Context
|
13
14
|
|
14
15
|
## Installation
|
15
16
|
|
@@ -34,9 +35,7 @@ function Counter() {
|
|
34
35
|
return (
|
35
36
|
<div>
|
36
37
|
<p>Count: {count()}</p>
|
37
|
-
<button onClick={() => count(prev => prev + 1)}>
|
38
|
-
Increment
|
39
|
-
</button>
|
38
|
+
<button onClick={() => count(prev => prev + 1)}>Increment</button>
|
40
39
|
</div>
|
41
40
|
);
|
42
41
|
}
|
@@ -44,11 +43,13 @@ function Counter() {
|
|
44
43
|
|
45
44
|
### Sharing State Between Components
|
46
45
|
|
46
|
+
#### Using Direct Reference
|
47
|
+
|
47
48
|
```tsx
|
48
49
|
import { reallyReactiveVal } from 'react-reactive-val';
|
49
50
|
|
50
51
|
// Create a shared reactive value
|
51
|
-
const sharedCount = reallyReactiveVal(0);
|
52
|
+
const [sharedCount, CountProvider, useSharedCount] = reallyReactiveVal(0);
|
52
53
|
|
53
54
|
function CounterDisplay() {
|
54
55
|
return <div>Count: {sharedCount()}</div>;
|
@@ -57,12 +58,41 @@ function CounterDisplay() {
|
|
57
58
|
function CounterButtons() {
|
58
59
|
return (
|
59
60
|
<div>
|
60
|
-
<button onClick={() => sharedCount(prev => prev + 1)}>
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
<button onClick={() => sharedCount(prev => prev + 1)}>Increment</button>
|
62
|
+
<button onClick={() => sharedCount(prev => prev - 1)}>Decrement</button>
|
63
|
+
</div>
|
64
|
+
);
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
68
|
+
#### Using Context (New in v2)
|
69
|
+
|
70
|
+
```tsx
|
71
|
+
import { reallyReactiveVal } from 'react-reactive-val';
|
72
|
+
|
73
|
+
// Create a reactive value with context
|
74
|
+
const [countValue, CountProvider, useCountContext] = reallyReactiveVal(0);
|
75
|
+
|
76
|
+
function App() {
|
77
|
+
return (
|
78
|
+
<CountProvider>
|
79
|
+
<CounterDisplay />
|
80
|
+
<CounterButtons />
|
81
|
+
</CountProvider>
|
82
|
+
);
|
83
|
+
}
|
84
|
+
|
85
|
+
function CounterDisplay() {
|
86
|
+
const count = useCountContext();
|
87
|
+
return <div>Count: {count()}</div>;
|
88
|
+
}
|
89
|
+
|
90
|
+
function CounterButtons() {
|
91
|
+
const count = useCountContext();
|
92
|
+
return (
|
93
|
+
<div>
|
94
|
+
<button onClick={() => count(prev => prev + 1)}>Increment</button>
|
95
|
+
<button onClick={() => count(prev => prev - 1)}>Decrement</button>
|
66
96
|
</div>
|
67
97
|
);
|
68
98
|
}
|
@@ -83,14 +113,17 @@ const value = useReactiveValue(initialValue);
|
|
83
113
|
|
84
114
|
### `reallyReactiveVal<T>(initialValue: T)`
|
85
115
|
|
86
|
-
Creates a standalone reactive value that can be used across components.
|
116
|
+
Creates a standalone reactive value that can be used across components. In v2, it returns a tuple containing the reactive value function, a Context Provider component, and a custom hook for accessing the value through context.
|
87
117
|
|
88
118
|
```tsx
|
89
|
-
const value = reallyReactiveVal(initialValue);
|
119
|
+
const [value, Provider, useValue] = reallyReactiveVal(initialValue);
|
90
120
|
```
|
91
121
|
|
92
122
|
- `initialValue`: The initial value of the reactive state
|
93
|
-
- Returns: A
|
123
|
+
- Returns: A tuple containing:
|
124
|
+
1. A function that can both read and update the value
|
125
|
+
2. A Context Provider component for wrapping consumers
|
126
|
+
3. A custom hook for accessing the value within the Context
|
94
127
|
|
95
128
|
### Usage with Value Getter/Setter
|
96
129
|
|
@@ -117,7 +150,10 @@ const count = useReactiveValue(0); // type: number
|
|
117
150
|
const user = useReactiveValue<User | null>(null);
|
118
151
|
|
119
152
|
// Type checking for updates
|
120
|
-
user(prev => ({ ...prev, name:
|
153
|
+
user(prev => ({ ...prev, name: 'John' })); // Type safe!
|
154
|
+
|
155
|
+
// With Context
|
156
|
+
const [userValue, UserProvider, useUser] = reallyReactiveVal<User | null>(null);
|
121
157
|
```
|
122
158
|
|
123
159
|
## Contributing
|
@@ -125,9 +161,9 @@ user(prev => ({ ...prev, name: "John" })); // Type safe!
|
|
125
161
|
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
126
162
|
|
127
163
|
1. Fork the repository
|
128
|
-
2. Create your feature branch (
|
129
|
-
3. Commit your changes (
|
130
|
-
4. Push to the branch (
|
164
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
165
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
166
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
131
167
|
5. Open a Pull Request
|
132
168
|
|
133
169
|
## License
|
@@ -136,4 +172,4 @@ This project is licensed under the ISC License - see the [LICENSE](LICENSE) file
|
|
136
172
|
|
137
173
|
## Author
|
138
174
|
|
139
|
-
Jatin Parate ([@jatin4228](https://github.com/jatin4228))
|
175
|
+
Jatin Parate ([@jatin4228](https://github.com/jatin4228))
|
package/dist/index.cjs.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
/*! For license information please see index.cjs.js.LICENSE.txt */
|
2
|
-
(()=>{"use strict";var e={698:(e,r)=>{var t=Symbol.for("react.transitional.element");function n(e,r,n){var o=null;if(void 0!==n&&(o=""+n),void 0!==r.key&&(o=""+r.key),"key"in r)for(var u in n={},r)"key"!==u&&(n[u]=r[u]);else n=r;return r=n.ref,{$$typeof:t,type:e,key:o,ref:void 0!==r?r:null,props:n}}Symbol.for("react.fragment"),r.jsx=n},848:(e,r,t)=>{e.exports=t(698)}},r={};function t(n){var o=r[n];if(void 0!==o)return o.exports;var u=r[n]={exports:{}};return e[n](u,u.exports,t),u.exports}t.d=(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},t.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};t.r(n),t.d(n,{default:()=>i,reallyReactiveVal:()=>a});const o=require("react");var u=t(848);function i(e){return(0,o.useMemo)((function(){return a(e)}),[e])}var a=function(e){var r=e,t=new Set,n=(0,o.memo)((function(){return(0,o.useSyncExternalStore)((function(e){return t.add(e),function(){return t.delete(e)}}),(function(){return r}))}));
|
2
|
+
(()=>{"use strict";var e={698:(e,r)=>{var t=Symbol.for("react.transitional.element");function n(e,r,n){var o=null;if(void 0!==n&&(o=""+n),void 0!==r.key&&(o=""+r.key),"key"in r)for(var u in n={},r)"key"!==u&&(n[u]=r[u]);else n=r;return r=n.ref,{$$typeof:t,type:e,key:o,ref:void 0!==r?r:null,props:n}}Symbol.for("react.fragment"),r.jsx=n},848:(e,r,t)=>{e.exports=t(698)}},r={};function t(n){var o=r[n];if(void 0!==o)return o.exports;var u=r[n]={exports:{}};return e[n](u,u.exports,t),u.exports}t.d=(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},t.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};t.r(n),t.d(n,{default:()=>i,reallyReactiveVal:()=>a});const o=require("react");var u=t(848);function i(e){return(0,o.useMemo)((function(){return a(e)}),[e])}var a=function(e){var r=e,t=new Set,n=(0,o.memo)((function(){return(0,o.useSyncExternalStore)((function(e){return t.add(e),function(){return t.delete(e)}}),(function(){return r}))}));function i(e){if(void 0===e)return(0,u.jsx)(n,{});r="function"==typeof e?e(r):e,t.forEach((function(e){e()}))}var a=(0,o.createContext)(i);return[i,(0,o.memo)((function(){return(0,u.jsx)(a.Provider,{value:i})})),function(){return(0,o.useContext)(a)}]};module.exports=n})();
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type ReactNode, type ReactElement } from
|
2
|
-
export default function useReactiveValue<T extends ReactNode>(initialValue: T): <U extends (T | ((currVal: T) => T)) | unknown | undefined>(updater?: U | undefined) => U extends T | ((currVal: T) => T) ? undefined : ReactElement<unknown, string | import("react").JSXElementConstructor<any
|
3
|
-
export declare const reallyReactiveVal: <T extends ReactNode>(initialValue: T) => <U extends (T | ((currVal: T) => T)) | unknown | undefined>(updater?: U) => U extends T | ((currVal: T) => T) ? undefined : ReactElement;
|
1
|
+
import { type ReactNode, type ReactElement } from 'react';
|
2
|
+
export default function useReactiveValue<T extends ReactNode>(initialValue: T): (import("react").NamedExoticComponent<object> | (<U extends (T | ((currVal: T) => T)) | unknown | undefined>(updater?: U | undefined) => U extends T | ((currVal: T) => T) ? undefined : ReactElement<unknown, string | import("react").JSXElementConstructor<any>>) | (() => <U extends (T | ((currVal: T) => T)) | unknown | undefined>(updater?: U | undefined) => U extends T | ((currVal: T) => T) ? undefined : ReactElement<unknown, string | import("react").JSXElementConstructor<any>>))[];
|
3
|
+
export declare const reallyReactiveVal: <T extends ReactNode>(initialValue: T) => ((<U extends (T | ((currVal: T) => T)) | unknown | undefined>(updater?: U) => U extends T | ((currVal: T) => T) ? undefined : ReactElement) | import("react").NamedExoticComponent<object> | (() => <U extends (T | ((currVal: T) => T)) | unknown | undefined>(updater?: U) => U extends T | ((currVal: T) => T) ? undefined : ReactElement))[];
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EACd,KAAK,YAAY,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EACd,KAAK,YAAY,EAMlB,MAAM,OAAO,CAAC;AAGf,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,CAAC,SAAS,SAAS,EAAE,YAAY,EAAE,CAAC,qDAsB5D,CAAC,SAAS,4BAAU,OAAO,GAAG,SAAS,oKAAvC,CAAC,SAAS,4BAAU,OAAO,GAAG,SAAS,6JApBvD;AAED,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,SAAS,EAAE,cAAc,CAAC,QAkBrD,CAAC,SAAS,gBAFK,CAAC,KAAK,CAAC,KAEF,OAAO,GAAG,SAAS,YAC1C,CAAC,KACR,CAAC,wBAJwB,CAAC,KAAK,CAAC,IAIZ,SAAS,GAAG,YAAY,2DAFlC,CAAC,SAAS,gBAFK,CAAC,KAAK,CAAC,KAEF,OAAO,GAAG,SAAS,YAC1C,CAAC,KACR,CAAC,wBAJwB,CAAC,KAAK,CAAC,IAIZ,SAAS,GAAG,YAAY,IA4BlD,CAAC"}
|
package/dist/index.esm.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
/*! For license information please see index.esm.js.LICENSE.txt */
|
2
|
-
import*as e from"react";var r={698:(e,r)=>{var t=Symbol.for("react.transitional.element");function n(e,r,n){var o=null;if(void 0!==n&&(o=""+n),void 0!==r.key&&(o=""+r.key),"key"in r)for(var u in n={},r)"key"!==u&&(n[u]=r[u]);else n=r;return r=n.ref,{$$typeof:t,type:e,key:o,ref:void 0!==r?r:null,props:n}}Symbol.for("react.fragment"),r.jsx=n},848:(e,r,t)=>{e.exports=t(698)}},t={};function n(e){var o=t[e];if(void 0!==o)return o.exports;var u=t[e]={exports:{}};return r[e](u,u.exports,n),u.exports}n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r);const o=(e=>{var r={};return n.d(r,e),r})({memo:()=>e.memo,useMemo:()=>e.useMemo,useSyncExternalStore:()=>e.useSyncExternalStore});var u=n(848);function a(e){return(0,o.useMemo)((function(){return f(e)}),[e])}var f=function(e){var r=e,t=new Set,n=(0,o.memo)((function(){return(0,o.useSyncExternalStore)((function(e){return t.add(e),function(){return t.delete(e)}}),(function(){return r}))}));
|
2
|
+
import*as e from"react";var r={698:(e,r)=>{var t=Symbol.for("react.transitional.element");function n(e,r,n){var o=null;if(void 0!==n&&(o=""+n),void 0!==r.key&&(o=""+r.key),"key"in r)for(var u in n={},r)"key"!==u&&(n[u]=r[u]);else n=r;return r=n.ref,{$$typeof:t,type:e,key:o,ref:void 0!==r?r:null,props:n}}Symbol.for("react.fragment"),r.jsx=n},848:(e,r,t)=>{e.exports=t(698)}},t={};function n(e){var o=t[e];if(void 0!==o)return o.exports;var u=t[e]={exports:{}};return r[e](u,u.exports,n),u.exports}n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r);const o=(e=>{var r={};return n.d(r,e),r})({createContext:()=>e.createContext,memo:()=>e.memo,useContext:()=>e.useContext,useMemo:()=>e.useMemo,useSyncExternalStore:()=>e.useSyncExternalStore});var u=n(848);function a(e){return(0,o.useMemo)((function(){return f(e)}),[e])}var f=function(e){var r=e,t=new Set,n=(0,o.memo)((function(){return(0,o.useSyncExternalStore)((function(e){return t.add(e),function(){return t.delete(e)}}),(function(){return r}))}));function a(e){if(void 0===e)return(0,u.jsx)(n,{});r="function"==typeof e?e(r):e,t.forEach((function(e){e()}))}var f=(0,o.createContext)(a);return[a,(0,o.memo)((function(){return(0,u.jsx)(f.Provider,{value:a})})),function(){return(0,o.useContext)(f)}]};export{a as default,f as reallyReactiveVal};
|
package/dist/index.umd.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
/*! For license information please see index.umd.js.LICENSE.txt */
|
2
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define("ReactReactiveVal",["react"],t):"object"==typeof exports?exports.ReactReactiveVal=t(require("react")):e.ReactReactiveVal=t(e.React)}(this,(e=>(()=>{"use strict";var t={442:t=>{t.exports=e},698:(e,t)=>{var r=Symbol.for("react.transitional.element");function o(e,t,o){var n=null;if(void 0!==o&&(n=""+o),void 0!==t.key&&(n=""+t.key),"key"in t)for(var
|
2
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define("ReactReactiveVal",["react"],t):"object"==typeof exports?exports.ReactReactiveVal=t(require("react")):e.ReactReactiveVal=t(e.React)}(this,(e=>(()=>{"use strict";var t={442:t=>{t.exports=e},698:(e,t)=>{var r=Symbol.for("react.transitional.element");function o(e,t,o){var n=null;if(void 0!==o&&(n=""+o),void 0!==t.key&&(n=""+t.key),"key"in t)for(var u in o={},t)"key"!==u&&(o[u]=t[u]);else o=t;return t=o.ref,{$$typeof:r,type:e,key:n,ref:void 0!==t?t:null,props:o}}Symbol.for("react.fragment"),t.jsx=o},848:(e,t,r)=>{e.exports=r(698)}},r={};function o(e){var n=r[e];if(void 0!==n)return n.exports;var u=r[e]={exports:{}};return t[e](u,u.exports,o),u.exports}o.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return o.d(t,{a:t}),t},o.d=(e,t)=>{for(var r in t)o.o(t,r)&&!o.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};o.r(n),o.d(n,{default:()=>i,reallyReactiveVal:()=>f});var u=o(442),a=o(848);function i(e){return(0,u.useMemo)((function(){return f(e)}),[e])}var f=function(e){var t=e,r=new Set,o=(0,u.memo)((function(){return(0,u.useSyncExternalStore)((function(e){return r.add(e),function(){return r.delete(e)}}),(function(){return t}))}));function n(e){if(void 0===e)return(0,a.jsx)(o,{});t="function"==typeof e?e(t):e,r.forEach((function(e){e()}))}var i=(0,u.createContext)(n);return[n,(0,u.memo)((function(){return(0,a.jsx)(i.Provider,{value:n})})),function(){return(0,u.useContext)(i)}]};return n})()));
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-reactive-val",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0",
|
4
4
|
"description": "A lightweight, type-safe reactive value management library for React applications",
|
5
5
|
"main": "dist/index.cjs.js",
|
6
6
|
"module": "dist/index.esm.js",
|
@@ -22,10 +22,22 @@
|
|
22
22
|
"build": "rm -rf dist && npm run build:types && npm run build:webpack",
|
23
23
|
"prepublishOnly": "npm run build",
|
24
24
|
"test": "echo \"Error: no test specified\" && exit 1",
|
25
|
-
"lint": "eslint
|
25
|
+
"lint": "eslint src --ext .ts,.tsx",
|
26
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
26
27
|
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
|
28
|
+
"format:check": "prettier --check \"src/**/*.{ts,tsx}\"",
|
29
|
+
"lint-staged": "lint-staged",
|
27
30
|
"prepare": "husky install"
|
28
31
|
},
|
32
|
+
"lint-staged": {
|
33
|
+
"src/**/*.{ts,tsx}": [
|
34
|
+
"eslint --fix",
|
35
|
+
"prettier --write"
|
36
|
+
],
|
37
|
+
"*.{json,md}": [
|
38
|
+
"prettier --write"
|
39
|
+
]
|
40
|
+
},
|
29
41
|
"repository": {
|
30
42
|
"type": "git",
|
31
43
|
"url": "git+https://github.com/jatin-parate/react-reactive-val.git"
|
@@ -58,6 +70,7 @@
|
|
58
70
|
"babel-loader": "^9.1.3",
|
59
71
|
"eslint": "^8.57.1",
|
60
72
|
"eslint-config-prettier": "^9.1.0",
|
73
|
+
"eslint-plugin-prettier": "^5.4.0",
|
61
74
|
"eslint-plugin-react": "^7.37.5",
|
62
75
|
"eslint-plugin-react-hooks": "^4.6.2",
|
63
76
|
"husky": "^9.1.7",
|
@@ -68,11 +81,5 @@
|
|
68
81
|
"typescript": "^5.8.3",
|
69
82
|
"webpack": "^5.90.3",
|
70
83
|
"webpack-cli": "^5.1.4"
|
71
|
-
},
|
72
|
-
"lint-staged": {
|
73
|
-
"*.{ts,tsx}": [
|
74
|
-
"eslint --fix",
|
75
|
-
"prettier --write"
|
76
|
-
]
|
77
84
|
}
|
78
85
|
}
|