Skip to content

Commit a1d395d

Browse files
committed
Updated examples and readme
1 parent cc75906 commit a1d395d

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,39 @@ npm install --save react-context-session
1313
import React from "react";
1414
import {ProvideSession, useSession} from "react-context-session";
1515

16+
// Arbitrary session data structure
1617
type MySessionType = {
1718
x: number;
1819
y: number;
1920
z: string;
2021
}
2122

23+
// Create type strict hook alias
2224
const useMySession = useSession<MySessionType>();
2325

26+
// Read usage
2427
function MyCalculation() {
2528
const [{x,y}] = useMySession(["x", "y"]);
2629

2730
return <p>{x} + {y} = {x + y}</p>;
2831
}
2932

33+
// Read usage
3034
function MyMessage() {
3135
const [{z}] = useMySession(["z"]);
3236

3337
return <p>{z}</p>;
3438
}
3539

40+
// Write usage
3641
function MyButton() {
3742
const [, setValue] = useMySession();
3843
return <button onClick={() => setValue("z", "My new string")}>Click me</button>;
3944
}
4045

46+
// Setup session in the root of your application, or at least higher in
47+
// the component hierarchy than the component using the `useSession` hook.
48+
// * Mandatory: Set default session values in the data prop
4149
function MyApp() {
4250
return (
4351
<ProvideSession data={{x: 5, y: 10, z: "My string"}}>
@@ -47,10 +55,35 @@ function MyApp() {
4755
</ProvideSession>
4856
)
4957
}
58+
```
5059

51-
export default MyApp;
52-
60+
## Multiple contexts
61+
If your app has different sections where the session data should to be shared between, you can set up different session providers
62+
for each section as following.
63+
```tsx
64+
function AdminSection() {
65+
return (
66+
<ProvideSession data={...} context={"admin"}>...</ProvideSession>
67+
)
68+
}
69+
function UserSection() {
70+
return (
71+
<ProvideSession data={...} context={"user"}>...</ProvideSession>
72+
)
73+
}
74+
```
5375

76+
## Save session data to storage
77+
react-context-session has no support for saving to storage, but do instead have a callback function for every time the data changes.
78+
Please notice that the callback only calls with updated data to its context.
79+
```tsx
80+
function MyProvider() {
81+
return (
82+
<ProvideSession data={...} onChange={data => {
83+
console.log("Your storage implementation here", data);
84+
}}>...</ProvideSession>
85+
)
86+
}
5487
```
5588

5689
## License

example/src/App.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ export function Child({ property }: { property: keyof Test }) {
4545
<div className={'card'}>
4646
<h3>{`Child (${property.toUpperCase()})`}</h3>
4747
<p className={'last-render'}>
48-
Last <u>effect</u> at: {updatedAt.getTime()}
48+
Last <u>render</u> at: {new Date().getTime()}
49+
</p>
50+
<p className={'last-effect'}>
51+
Last <u>effect</u> on <b>{property.toUpperCase()}</b> at: {updatedAt.getTime()}
4952
</p>
5053
<div className={'row'}>
5154
<div className={'states'}>
@@ -76,6 +79,9 @@ export function Parent() {
7679
<div className={'parent'}>
7780
<div className={'card'}>
7881
<h3>Parent</h3>
82+
<p className={'last-render'}>
83+
Last <u>render</u> at: {new Date().getTime()}
84+
</p>
7985
<div className={'row'}>
8086
<div className={'states'}>
8187
<p>
@@ -139,9 +145,6 @@ const App = () => {
139145
return (
140146
<div>
141147
<h1>react-context-session</h1>
142-
<p className={'sub-headline'}>
143-
Multiple session contexts, render optimized and type strict
144-
</p>
145148
<div className={'container'}>
146149
<div className={'context'}>
147150
<ProvideSession

example/src/getting-started.tsx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,40 @@ import React from 'react'
22
import { ProvideSession, useSession } from 'react-context-session'
33

44
type MySessionType = {
5-
x: number
6-
y: number
7-
z: string
5+
x: number;
6+
y: number;
7+
z: string;
88
}
99

10-
const useMySession = useSession<MySessionType>()
10+
// Create type strict hook alias
11+
const useMySession = useSession<MySessionType>();
1112

13+
// Read usage
1214
function MyCalculation() {
13-
const [{ x, y }] = useMySession(['x', 'y'])
15+
const [{x,y}] = useMySession(["x", "y"]);
1416

15-
return (
16-
<p>
17-
{x} + {y} = {x + y}
18-
</p>
19-
)
17+
return <p>{x} + {y} = {x + y}</p>;
2018
}
2119

20+
// Read usage
2221
function MyMessage() {
23-
const [{ z }] = useMySession(['z'])
22+
const [{z}] = useMySession(["z"]);
2423

25-
return <p>{z}</p>
24+
return <p>{z}</p>;
2625
}
2726

27+
// Write usage
2828
function MyButton() {
29-
const [, setValue] = useMySession()
30-
return (
31-
<button onClick={() => setValue('z', 'My new string')}>Click me</button>
32-
)
29+
const [, setValue] = useMySession();
30+
return <button onClick={() => setValue("z", "My new string")}>Click me</button>;
3331
}
3432

33+
// Setup session in the root of your application, or at least higher in
34+
// the component hierarchy than the component using the `useSession` hook.
35+
// * Mandatory: Set default session values in the data prop
3536
function MyApp() {
3637
return (
37-
<ProvideSession data={{ x: 5, y: 10, z: 'My string' }}>
38+
<ProvideSession data={{x: 5, y: 10, z: "My string"}}>
3839
<MyCalculation />
3940
<MyMessage />
4041
<MyButton />

example/src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import './index.css'
22

33
import React from 'react'
44
import ReactDOM from 'react-dom'
5-
//import App from './App'
6-
import MyApp from './getting-started'
5+
import App from './App'
6+
//import MyApp from './getting-started'
77

8-
ReactDOM.render(<MyApp />, document.getElementById('root'))
8+
ReactDOM.render(<App />, document.getElementById('root'))

0 commit comments

Comments
 (0)