react-usectx 1.0.0 → 1.0.4

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 CHANGED
@@ -1,2 +1,79 @@
1
1
  # React-Context
2
+
2
3
  Custom React globally accessible context hook
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ npm i react-usectx --save
9
+ ```
10
+
11
+ ## Add it to your project
12
+
13
+ ```js
14
+ import useCtx from "react-usectx";
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```js
20
+ // get both current state and set function
21
+ const [myContext, setMyContext] = useCtx("stateName");
22
+ ```
23
+
24
+ ## Other methods:
25
+
26
+ ### getCtx
27
+
28
+ ```js
29
+ // get current state only
30
+
31
+ import { getCtx } from "react-usectx";
32
+
33
+ const myContext = getCtx("stateName");
34
+ ```
35
+
36
+ ### updateCtx
37
+
38
+ ```js
39
+ // set function only
40
+
41
+ import { updateCtx } from "react-usectx";
42
+
43
+ const setMyContext = updateCtx("stateName");
44
+
45
+ setMyContext({ name: "Rick Ross" });
46
+ ```
47
+
48
+ ## Example
49
+
50
+ ```js
51
+ function Page() {
52
+ const [title, setTitle] = useCtx("title");
53
+
54
+ return (
55
+ <div className="wrapper">
56
+ <form>
57
+ <label htmlFor="text">Enter text</label>
58
+ <input
59
+ type="text"
60
+ id="text"
61
+ onChange={(e) => setTitle(e.target.value)}
62
+ />
63
+ </form>
64
+ <Title></Title>
65
+ <p>{title}</p>
66
+ </div>
67
+ );
68
+ }
69
+
70
+ function Title() {
71
+ const title: string = getContext("title");
72
+ return (
73
+ <h1>
74
+ Component: <br />
75
+ <strong>{title}</strong>
76
+ </h1>
77
+ );
78
+ }
79
+ ```
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { useCtx as default } from "./src/context";
2
+ export { getCtx } from "./src/context";
3
+ export { updateCtx } from "./src/context";
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "react-usectx",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "main": "index.js",
5
+ "type": "module",
5
6
  "scripts": {
6
- "test": "jest"
7
+ "test": "jest",
8
+ "build": "npx tsc index.ts --target es2017"
7
9
  },
8
10
  "author": "https://jsf7.dev",
9
11
  "license": "MIT",
@@ -12,10 +14,14 @@
12
14
  "react": "^19.1.0"
13
15
  },
14
16
  "devDependencies": {
17
+ "@babel/parser": "^7.28.0",
15
18
  "@babel/preset-env": "^7.28.0",
16
19
  "@babel/preset-typescript": "^7.27.1",
17
20
  "@types/react": "^19.1.8",
18
21
  "jest": "^30.0.4",
19
- "typescript": "^5.8.3"
22
+ "ts-node": "^10.9.2",
23
+ "typescript": "^5.8.3",
24
+ "undici": "^7.11.0",
25
+ "undici-types": "^7.11.0"
20
26
  }
21
27
  }
package/src/context.js ADDED
@@ -0,0 +1,25 @@
1
+ 'use client';
2
+ import { useSyncExternalStore } from 'react';
3
+ import { CreateContext } from './create-context';
4
+ const cache = {};
5
+ const getInstance = (name) => {
6
+ if (!cache[name]) {
7
+ cache[name] = new CreateContext();
8
+ }
9
+ return cache[name];
10
+ };
11
+ export function updateCtx(name) {
12
+ const ctx = getInstance(name);
13
+ return ctx.commit.bind(ctx);
14
+ }
15
+ export function getCtx(name) {
16
+ const context = getInstance(name);
17
+ const subscription = (callback) => {
18
+ context.subscribe(callback);
19
+ return () => context.unsubscribe(callback);
20
+ };
21
+ return useSyncExternalStore(subscription, context.getState.bind(context), context.getState.bind(context));
22
+ }
23
+ export function useCtx(name) {
24
+ return [getCtx(name), updateCtx(name)];
25
+ }
@@ -0,0 +1,38 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _CreateContext_eventHandlers, _CreateContext_state;
13
+ export class CreateContext {
14
+ constructor() {
15
+ _CreateContext_eventHandlers.set(this, []);
16
+ _CreateContext_state.set(this, null);
17
+ }
18
+ subscribe(handler) {
19
+ __classPrivateFieldGet(this, _CreateContext_eventHandlers, "f").push(handler);
20
+ }
21
+ unsubscribe(handler) {
22
+ const index = __classPrivateFieldGet(this, _CreateContext_eventHandlers, "f").indexOf(handler);
23
+ if (index !== -1) {
24
+ __classPrivateFieldGet(this, _CreateContext_eventHandlers, "f").splice(index, 1);
25
+ }
26
+ }
27
+ commit(state) {
28
+ this.setState(state);
29
+ __classPrivateFieldGet(this, _CreateContext_eventHandlers, "f").forEach((callback) => callback.call(undefined, state));
30
+ }
31
+ setState(state) {
32
+ __classPrivateFieldSet(this, _CreateContext_state, state, "f");
33
+ }
34
+ getState() {
35
+ return __classPrivateFieldGet(this, _CreateContext_state, "f");
36
+ }
37
+ }
38
+ _CreateContext_eventHandlers = new WeakMap(), _CreateContext_state = new WeakMap();
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2016",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "moduleResolution": "node",
10
+ "paths": {
11
+ // This is solely to stop a bug with @types/node as of 12/15/2023
12
+ "undici-types": [
13
+ "./node_modules/undici-types/index.d.ts"
14
+ ],
15
+ }
16
+ },
17
+ "display": "Recommended",
18
+ "$schema": "https://json.schemastore.org/tsconfig"
19
+ }
File without changes