react-usectx 1.2.0 → 1.2.1
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 +19 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,46 +11,46 @@ npm i react-usectx --save
|
|
|
11
11
|
## Add it to your project
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
import
|
|
14
|
+
import {useGlobalState} from "react-usectx";
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
```js
|
|
20
20
|
// get both current state and set function
|
|
21
|
-
const [
|
|
21
|
+
const [myState, setMyState] = useGlobalState("stateName");
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
## Other methods:
|
|
25
25
|
|
|
26
|
-
###
|
|
26
|
+
### getGlobalState
|
|
27
27
|
|
|
28
28
|
```js
|
|
29
29
|
// get current state only
|
|
30
30
|
|
|
31
|
-
import {
|
|
31
|
+
import { getGlobalState } from "react-usectx";
|
|
32
32
|
|
|
33
|
-
const
|
|
33
|
+
const myState = getGlobalState("stateName");
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
###
|
|
36
|
+
### updateGlobalState
|
|
37
37
|
|
|
38
38
|
```js
|
|
39
39
|
// set function only
|
|
40
40
|
|
|
41
|
-
import {
|
|
41
|
+
import { updateGlobalState } from "react-usectx";
|
|
42
42
|
|
|
43
|
-
const
|
|
43
|
+
const setMyState = updateGlobalState("stateName");
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
setMyState({ name: "Rick Ross" });
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
###
|
|
48
|
+
### useGlobalReducer
|
|
49
49
|
|
|
50
50
|
```js
|
|
51
|
-
import {
|
|
51
|
+
import { useGlobalReducer } from "react-usectx";
|
|
52
52
|
|
|
53
|
-
const fullName =
|
|
53
|
+
const fullName = useGlobalReducer("stateName", (data) => {
|
|
54
54
|
return `${data.firstName} ${data.lastName}`;
|
|
55
55
|
});
|
|
56
56
|
```
|
|
@@ -62,19 +62,19 @@ const fullName = useReducer("stateName", (data) => {
|
|
|
62
62
|
```js
|
|
63
63
|
const myContextID = "MyUniqueIdentifier";
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
useGlobalState(myContextID);
|
|
66
66
|
```
|
|
67
67
|
|
|
68
68
|
The stateName serves as a unique identifier for a shared state object. Internally, the state manager maintains a list of state entries, each associated with a distinct stateName.
|
|
69
69
|
|
|
70
|
-
When any of the provided functions—
|
|
70
|
+
When any of the provided functions—useGlobalState, updateGlobalState, or getGlobalState—are invoked, the state manager checks whether a state object with the specified stateName already exists. If it does not, a new state entry is automatically created and initialized.
|
|
71
71
|
|
|
72
|
-
This mechanism enables global state sharing across components. For example, calling
|
|
72
|
+
This mechanism enables global state sharing across components. For example, calling getGlobalState("example-1") in one component will return the same state reference when called with "example-1" in any other component within the same document. This allows for seamless state synchronization across different parts of your application without the need for explicit context providers.
|
|
73
73
|
|
|
74
74
|
### initialState
|
|
75
75
|
|
|
76
76
|
```js
|
|
77
|
-
|
|
77
|
+
useGlobalState(myContextID, { name: "John" });
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
The value you want the state to be initially. It can be a value of any type.
|
|
@@ -83,7 +83,7 @@ The value you want the state to be initially. It can be a value of any type.
|
|
|
83
83
|
|
|
84
84
|
```js
|
|
85
85
|
function Page() {
|
|
86
|
-
const [title, setTitle] =
|
|
86
|
+
const [title, setTitle] = useGlobalState("title");
|
|
87
87
|
|
|
88
88
|
return (
|
|
89
89
|
<div className="wrapper">
|
|
@@ -116,7 +116,7 @@ function Title() {
|
|
|
116
116
|
|
|
117
117
|
```js
|
|
118
118
|
function Page() {
|
|
119
|
-
const [title, setTitle] =
|
|
119
|
+
const [title, setTitle] = useGlobalState("title");
|
|
120
120
|
|
|
121
121
|
return (
|
|
122
122
|
<div className="wrapper">
|
|
@@ -135,7 +135,7 @@ function Page() {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
function Title() {
|
|
138
|
-
const reducedTitle: string =
|
|
138
|
+
const reducedTitle: string = useGlobalReducer(
|
|
139
139
|
"title",
|
|
140
140
|
(title) => `Title is: ${title.toUpperCase()}`
|
|
141
141
|
);
|