Skip to content

Commit dac6616

Browse files
committed
add ssr example
1 parent 610a72a commit dac6616

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

example/ssr/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel

example/ssr/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "ssr",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start"
9+
},
10+
"dependencies": {
11+
"next": "10.1.3",
12+
"react": "17.0.2",
13+
"react-dom": "17.0.2"
14+
}
15+
}

example/ssr/pages/_app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function MyApp({ Component, pageProps }) {
2+
return <Component {...pageProps} />
3+
}
4+
5+
export default MyApp

example/ssr/pages/index.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React, { useReducer } from "react";
2+
3+
const MyContext1 = React.createContext({});
4+
const MyContext2 = React.createContext({});
5+
6+
MyContext1.displayName = "MyContext1";
7+
MyContext2.displayName = "MyContext2";
8+
9+
10+
const ws = new WeakSet();
11+
const foo = {};
12+
13+
ws.add(foo);
14+
15+
let myMap = new Map()
16+
myMap.set("s", 'not a number')
17+
18+
const valuesToTest = {
19+
f: new Set([1,2, 3,3]),
20+
g: ws,
21+
h: myMap,
22+
i: ws,
23+
}
24+
25+
class Test2 extends React.Component {
26+
constructor(props) {
27+
super(props);
28+
29+
this.state = {
30+
counter5: 0,
31+
};
32+
}
33+
34+
render() {
35+
const { counter5 } = this.state;
36+
const { id } = this.props;
37+
// const { id } = this.context;
38+
39+
return (
40+
<MyContext2.Provider value={{ id: counter5, b: "world", ...valuesToTest }}>
41+
<button onClick={() => this.setState({ counter5: counter5 + 1 })}>
42+
Click 5 Me {counter5} {id}
43+
</button>
44+
</MyContext2.Provider>
45+
);
46+
}
47+
}
48+
49+
Test2.contextType = MyContext1;
50+
51+
function myReducer(state, action) {
52+
// counter++;
53+
// console.log(counter);
54+
switch (action.type) {
55+
case 'increment':
56+
return {count: state.count + 1};
57+
case 'decrement':
58+
return {count: state.count - 1};
59+
default:
60+
// throw new Error();
61+
}
62+
63+
return state;
64+
}
65+
66+
const initialState = {count: 0};
67+
68+
function Counter() {
69+
const [state, dispatch] = useReducer(myReducer, initialState);
70+
// const dd = useContext(MyContext1);
71+
// const [state2, dispatch3] = useReducer(reducer, initialState);
72+
// const [abcd, setabcd] = useState(2);
73+
74+
75+
return (
76+
<>
77+
{/* Count: {dd.id} */}
78+
Count: {state.count}
79+
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
80+
<button onClick={() => dispatch({type: 'increment'})}>+</button>
81+
</>
82+
);
83+
}
84+
85+
86+
export default function Home() {
87+
return (
88+
<div>
89+
<Test2 />
90+
<Counter />
91+
</div>
92+
)
93+
}

0 commit comments

Comments
 (0)