Skip to content

Commit 9023bf5

Browse files
authored
Merge pull request #10 from oslabs-beta/backend
Backend - Update for useState compatibility for NextJS/React
2 parents 611bf8c + e2a73c7 commit 9023bf5

File tree

32 files changed

+1521
-1194
lines changed

32 files changed

+1521
-1194
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ node_modules/
55
.DS_Store
66
src/extension/build/bundles
77
package/reactime-*.tgz
8-
tictactoe
98
parents
109
coverage
1110
src/extension/build.zip

demo-app-next/src/components/Buttons.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import Increment from "./Increment";
1+
import Increment from './Increment';
22

33
export default function Buttons() {
44
const buttons = [];
55
for (let i = 0; i < 4; i++) {
6-
buttons.push(<Increment />);
6+
buttons.push(<Increment key={i} />);
77
}
88

99
return (
10-
<div className="buttons">
10+
<div className='buttons'>
1111
<h1>Stateful Buttons</h1>
1212
<h4>
13-
These buttons are functional components that each manage their own state
14-
with the useState hook.
13+
These buttons are functional components that each manage their own state with the useState
14+
hook.
1515
</h4>
1616
{buttons}
1717
</div>

demo-app-next/src/components/navbar.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import Link from "next/link";
1+
import Link from 'next/link';
22

33
export default function Navbar() {
44
return (
5-
<div className="nav">
6-
<Link className="link" href="/">
5+
<div className='nav'>
6+
<Link className='link' href='/'>
77
About
88
</Link>
9-
<Link id="tictactoe" className="link" href="/tictactoe">
9+
<Link id='tictactoe' className='link' href='/tictactoe'>
1010
Tic-Tac-Toe
1111
</Link>
12-
<Link className="link" href="/buttons">
12+
<Link className='link' href='/buttons'>
1313
Counter
1414
</Link>
1515
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Board from '../../components/Board';
2+
import Navbar from '../../components/navbar';
3+
4+
export default function BoardPage() {
5+
return (
6+
<div>
7+
<Navbar />
8+
<Board />
9+
</div>
10+
);
11+
}

demo-app/src/client/Components/Buttons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Increment from './Increment';
44
function Buttons() {
55
const buttons = [];
66
for (let i = 0; i < 4; i++) {
7-
buttons.push(<Increment />);
7+
buttons.push(<Increment key={i} />);
88
}
99

1010
return (

demo-app/src/client/Components/Home.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1+
import { set } from 'lodash';
12
import React from 'react';
3+
import { useState } from 'react';
24

35
function Home() {
6+
const [dummyDummy1, setDummyData1] = useState('dummyData1');
7+
const [dummyDummy2, setDummyData2] = useState('dummyData2');
8+
9+
function handleClick1() {
10+
setDummyData1((dummyData1) => (dummyData1 === 'dummyData1' ? 'test1' : 'dummyData1'));
11+
}
12+
13+
function handleClick2() {
14+
setDummyData2((dummyData2) => (dummyData2 === 'dummyData2' ? 'test2' : 'dummyData2'));
15+
}
416
return (
517
<div className='about'>
6-
<h1>Lorem Ipsum</h1>
7-
<p>
18+
<p>{dummyDummy1}</p>
19+
<p>{dummyDummy2}</p>
20+
<button
21+
onClick={() => {
22+
handleClick1();
23+
handleClick2();
24+
}}
25+
>
26+
{' '}
27+
Test{' '}
28+
</button>
29+
{/* <h1>Lorem Ipsum</h1> */}
30+
{/* <p>
831
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
932
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
1033
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
@@ -17,7 +40,7 @@ function Home() {
1740
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
1841
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
1942
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
20-
</p>
43+
</p> */}
2144
</div>
2245
);
2346
}

demo-app/src/client/Components/Increment.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import React, { useState } from 'react';
2-
2+
import Box from './Box';
3+
import { BoardText } from '../../types';
4+
import { Function } from 'lodash';
35
function Increment() {
46
const [count, setCount] = useState(0);
5-
7+
// const [value, setValue]: [BoardText, any] = useState('-');
68
return (
7-
<button className='increment' onClick={() => setCount(count + 1)}>
8-
You clicked me {count} times.
9-
</button>
9+
<div>
10+
<button className='increment' onClick={() => setCount(count + 1)}>
11+
You clicked me {count} times.
12+
</button>
13+
{/* <div> */}
14+
{/* <Box
15+
value={value}
16+
column={3}
17+
row={2}
18+
handleBoxClick={() => setValue((value: BoardText) => (value == 'X' ? 'O' : 'X'))}
19+
/> */}
20+
{/* </div> */}
21+
</div>
1022
);
1123
}
1224

demo-app/src/client/Components/Nav.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function Nav() {
1313
<Link className='link' to='/buttons'>
1414
Counter
1515
</Link>
16+
<Link className='link' to='/test'>
17+
Test
18+
</Link>
1619
</div>
1720
);
1821
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import { useState, createContext, useContext } from 'react';
3+
4+
interface User {
5+
username: string;
6+
}
7+
8+
const defaultUser: User = { username: 'dummyusername' };
9+
10+
const UserContext = createContext<User>(defaultUser);
11+
12+
function Test() {
13+
const [user, setUser] = useState(defaultUser);
14+
return (
15+
<UserContext.Provider value={defaultUser}>
16+
<h1>{`What's up ${user.username}`}</h1>
17+
<Component2 />
18+
</UserContext.Provider>
19+
);
20+
}
21+
function Component2() {
22+
return (
23+
<>
24+
<h1>Component 2</h1>
25+
<Component3 />
26+
</>
27+
);
28+
}
29+
30+
function Component3() {
31+
return (
32+
<>
33+
<h1>Component 3</h1>
34+
<Component4 />
35+
</>
36+
);
37+
}
38+
39+
function Component4() {
40+
return (
41+
<>
42+
<h1>Component 4</h1>
43+
<Component5 />
44+
</>
45+
);
46+
}
47+
48+
function Component5() {
49+
const user = useContext(UserContext);
50+
51+
return (
52+
<>
53+
<h1>Component 5</h1>
54+
<h2>{`Hello ${user.username} again!`}</h2>
55+
</>
56+
);
57+
}
58+
59+
export default Test;

demo-app/src/client/Router.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import * as React from 'react';
22
import * as ReactDOM from 'react-dom';
3+
import { createRoot } from 'react-dom/client';
34
import { BrowserRouter, Routes, Route } from 'react-router-dom';
45
import Nav from './Components/Nav';
56
import Board from './Components/Board';
67
import Home from './Components/Home';
78
import Buttons from './Components/Buttons';
9+
import Test from './Components/Test';
810

9-
const root = document.getElementById('root');
11+
const domNode = document.getElementById('root');
12+
const root = createRoot(domNode);
1013

11-
ReactDOM.render(
14+
root.render(
1215
<BrowserRouter>
1316
<Nav />
1417
<Routes>
1518
<Route path='/tictactoe' element={<Board />} />
1619
<Route path='/' element={<Home />} />
1720
<Route path='/buttons' element={<Buttons />} />
21+
<Route path='/test' element={<Test />} />
1822
</Routes>
1923
</BrowserRouter>,
20-
root,
2124
);

0 commit comments

Comments
 (0)