Skip to content

Commit 8154529

Browse files
committed
Remove unnecessary validation and adjust documentation.
1 parent cabdb4b commit 8154529

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

src/index.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import React, {
4242
* return () => delValue();
4343
* }, []);
4444
*
45-
* return <>{JSON.stringify(value)}<>;
45+
* return <>{JSON.stringify(value)}</>;
4646
* };
4747
*
4848
* const ComponentB = () => {
@@ -57,7 +57,7 @@ import React, {
5757
* return () => delValue();
5858
* }, []);
5959
*
60-
* return <>{JSON.stringify(value)}<>;
60+
* return <>{JSON.stringify(value)}</>;
6161
* };
6262
*
6363
* const App = withStore(() => {
@@ -81,33 +81,60 @@ import React, {
8181
*
8282
* render(<App />, document.querySelector('#root'));
8383
*
84-
* @example <caption>Different React Context</caption>
84+
* @example <caption>Different React Context with localStorage</caption>
8585
* import { render } from 'react-dom';
8686
* 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';
9188
*
9289
* const ComponentA = () => {
93-
* const [value, setValue, delValue] = useLocalStore('key', 'default');
90+
* const { setStores } = useLocalStores();
9491
*
9592
* useEffect(() => {
96-
* setValue('value');
97-
* return () => delValue();
93+
* setStores({
94+
* key: 'value',
95+
* });
9896
* }, []);
9997
*
100-
* return <>{JSON.stringify(value)}<>;
98+
* return null;
10199
* };
102100
*
103101
* const ComponentB = () => {
104102
* const [value] = useLocalStore('key');
105-
* return <>{JSON.stringify(value)}<>;
103+
* return <>{JSON.stringify(value)}</>;
106104
* };
107105
*
108106
* const ComponentC = () => {
109107
* 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)}</>;
111138
* };
112139
*
113140
* render(<><ComponentA /><ComponentB /><ComponentC /></>, document.querySelector('#root'));
@@ -149,16 +176,14 @@ const storage = {
149176
return false;
150177
},
151178
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+
});
162187
},
163188
};
164189

0 commit comments

Comments
 (0)