@@ -42,7 +42,7 @@ import React, {
42
42
* return () => delValue();
43
43
* }, []);
44
44
*
45
- * return <>{JSON.stringify(value)}<>;
45
+ * return <>{JSON.stringify(value)}</ >;
46
46
* };
47
47
*
48
48
* const ComponentB = () => {
@@ -57,7 +57,7 @@ import React, {
57
57
* return () => delValue();
58
58
* }, []);
59
59
*
60
- * return <>{JSON.stringify(value)}<>;
60
+ * return <>{JSON.stringify(value)}</ >;
61
61
* };
62
62
*
63
63
* const App = withStore(() => {
@@ -81,33 +81,60 @@ import React, {
81
81
*
82
82
* render(<App />, document.querySelector('#root'));
83
83
*
84
- * @example <caption>Different React Context</caption>
84
+ * @example <caption>Different React Context with localStorage </caption>
85
85
* import { render } from 'react-dom';
86
86
* import { useEffect } from 'react';
87
- * import { useLocalStore } from 'react-store-context-hooks';
88
- *
89
- * // Simulate an existing value
90
- * localStorage.setItem('key', JSON.stringify('local-default'));
87
+ * import { useLocalStore, useLocalStores } from 'react-store-context-hooks';
91
88
*
92
89
* const ComponentA = () => {
93
- * const [value, setValue, delValue] = useLocalStore('key', 'default' );
90
+ * const { setStores } = useLocalStores( );
94
91
*
95
92
* useEffect(() => {
96
- * setValue('value');
97
- * return () => delValue();
93
+ * setStores({
94
+ * key: 'value',
95
+ * });
98
96
* }, []);
99
97
*
100
- * return <>{JSON.stringify(value)}<> ;
98
+ * return null ;
101
99
* };
102
100
*
103
101
* const ComponentB = () => {
104
102
* const [value] = useLocalStore('key');
105
- * return <>{JSON.stringify(value)}<>;
103
+ * return <>{JSON.stringify(value)}</ >;
106
104
* };
107
105
*
108
106
* const ComponentC = () => {
109
107
* const [value] = useLocalStore('key');
110
- * return <>{JSON.stringify(value)}<>;
108
+ * return <>{JSON.stringify(value)}</>;
109
+ * };
110
+ *
111
+ * render(<><ComponentA /><ComponentB /><ComponentC /></>, document.querySelector('#root'));
112
+ *
113
+ * @example <caption>Different React Context with sessionStorage</caption>
114
+ * import { render } from 'react-dom';
115
+ * import { useEffect } from 'react';
116
+ * import { useSessionStore, useSessionStores } from 'react-store-context-hooks';
117
+ *
118
+ * const ComponentA = () => {
119
+ * const { setStores } = useSessionStores();
120
+ *
121
+ * useEffect(() => {
122
+ * setStores({
123
+ * key: 'value',
124
+ * });
125
+ * }, []);
126
+ *
127
+ * return null;
128
+ * };
129
+ *
130
+ * const ComponentB = () => {
131
+ * const [value] = useSessionStore('key');
132
+ * return <>{JSON.stringify(value)}</>;
133
+ * };
134
+ *
135
+ * const ComponentC = () => {
136
+ * const [value] = useSessionStore('key');
137
+ * return <>{JSON.stringify(value)}</>;
111
138
* };
112
139
*
113
140
* render(<><ComponentA /><ComponentB /><ComponentC /></>, document.querySelector('#root'));
@@ -149,16 +176,14 @@ const storage = {
149
176
return false ;
150
177
} ,
151
178
sets: ( persistence : Storage , data : { [ string ] : mixed } ) => {
152
- if ( persistence instanceof Storage ) {
153
- const name = persistence === localStorage ? 'local' : 'session' ;
154
- Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
155
- if ( storage . set ( persistence , key , value ) ) {
156
- document . dispatchEvent ( new CustomEvent ( `${ name } Storage.setItem` , {
157
- detail : { key, value } ,
158
- } ) ) ;
159
- }
160
- } ) ;
161
- }
179
+ const name = persistence === localStorage ? 'local' : 'session' ;
180
+ Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
181
+ if ( storage . set ( persistence , key , value ) ) {
182
+ document . dispatchEvent ( new CustomEvent ( `${ name } Storage.setItem` , {
183
+ detail : { key, value } ,
184
+ } ) ) ;
185
+ }
186
+ } ) ;
162
187
} ,
163
188
} ;
164
189
0 commit comments