Skip to content

Commit fdf2ccd

Browse files
committed
Add remaining examples from React Review
1 parent 23ecdd7 commit fdf2ccd

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

src/App.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import './App.css';
22
import ExampleComponents from './ExampleComponents';
33
import ExampleConditionalRendering from './ExampleConditionalRendering';
4+
import ExampleConvertingClassComponentsToFunctionComponents from './ExampleConvertingClassComponentsToFunctionComponents';
5+
import ExampleFormsWithControlledComponents from './ExampleFormsWithControlledComponents';
6+
import ExampleLiftingStateUp from './ExampleLiftingStateUp';
47
import ExampleMappingOverArrays from './ExampleMappingOverArrays';
58
import ExampleProps from './ExampleProps';
69
import ExamplePropDestructuring from './ExamplePropsDestructuring';
710
import ExampleStateNotSynchronous from './ExampleSetStateIsNotSynchronous';
811
import ExampleStateCounter from './ExampleStateCounter';
912
import ExampleStateEmail from './ExampleStateEmail';
13+
import ExampleStyling from './ExampleStyling';
1014

1115
export default function App() {
1216
return (
@@ -24,6 +28,10 @@ export default function App() {
2428
<ExampleConditionalRendering />
2529
<ExamplePropDestructuring />
2630
<ExampleStateNotSynchronous />
31+
<ExampleFormsWithControlledComponents />
32+
<ExampleLiftingStateUp />
33+
<ExampleStyling />
34+
<ExampleConvertingClassComponentsToFunctionComponents />
2735
<br />
2836
<br />
2937
<br />
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { useState } from 'react';
2+
3+
// // Class components are old and no longer recommended
4+
// class ExampleClassToFunctionClass extends React.Component {
5+
// state = {
6+
// detailsShown: false,
7+
// };
8+
9+
// render() {
10+
// const { user } = this.props;
11+
12+
// if (user === undefined) return <div>User not found!</div>;
13+
14+
// return (
15+
// <>
16+
// <img src={user.image} alt={user.imageAlt} />
17+
// <h1>{user.name}</h1>
18+
// <button
19+
// onClick={() =>
20+
// this.setState({ detailsShown: !this.state.detailsShown })
21+
// }
22+
// >
23+
// {this.state.detailsShown ? 'Hide' : 'Show'} Details
24+
// </button>
25+
// {this.state.detailsShown && <p>{user.details}</p>}
26+
// </>
27+
// );
28+
// }
29+
// }
30+
31+
function ExampleClassToFunctionClass(props) {
32+
const [detailsShown, setDetailsShown] = useState(false);
33+
34+
if (props.user === undefined) return <div>User not found!</div>;
35+
36+
return (
37+
<>
38+
<img src={props.user.image} alt={props.user.imageAlt} />
39+
<h1>{props.user.name}</h1>
40+
<button onClick={() => setDetailsShown((prev) => !prev)}>
41+
{detailsShown ? 'Hide' : 'Show'} Details
42+
</button>
43+
{detailsShown && <p>{props.user.details}</p>}
44+
</>
45+
);
46+
}
47+
48+
export default function ExampleConvertingClassComponentsToFunctionComponents() {
49+
return (
50+
<div>
51+
<h1>ExampleConvertingClassComponentsToFunctionComponents</h1>
52+
<ExampleClassToFunctionClass
53+
user={{
54+
details: 'I like memes',
55+
name: 'Lukas',
56+
imageAlt: 'this is an image',
57+
image:
58+
'https://api.memegen.link/images/buzz/memes/memes_everywhere.png?width=400',
59+
}}
60+
/>
61+
</div>
62+
);
63+
}
64+
65+
// If you wanted to learn about class components, you could use a ChatGPT / Gemini / Claude prompt like this:
66+
67+
// I have a React class component that I would like to convert to a function component:
68+
//
69+
// ```
70+
// class ExampleClassToFunctionClass extends React.Component {
71+
// state = {
72+
// detailsShown: false,
73+
// };
74+
//
75+
// render() {
76+
// const { user } = this.props;
77+
//
78+
// if (user === undefined) return <div>User not found!</div>;
79+
//
80+
// return (
81+
// <>
82+
// <img src={user.image} alt={user.imageAlt} />
83+
// <h1>{user.name}</h1>
84+
// <button
85+
// onClick={() =>
86+
// this.setState({ detailsShown: !this.state.detailsShown })
87+
// }
88+
// >
89+
// {this.state.detailsShown ? 'Hide' : 'Show'} Details
90+
// </button>
91+
// {this.state.detailsShown && <p>{user.details}</p>}
92+
// </>
93+
// );
94+
// }
95+
// }
96+
// ```
97+
//
98+
// I am learning about class components. Can you guide me through (without telling me the answer) and show me how to convert it myself? Work step by step and ask me lots of questions.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useState } from 'react';
2+
3+
export default function ExampleFormsWithControlledComponents() {
4+
// 1. Create state variable
5+
const [username, setUsername] = useState('');
6+
7+
const [reversedUsername, setReversedUsername] = useState('');
8+
9+
return (
10+
<div>
11+
<h1>ExampleFormsWithControlledComponents</h1>
12+
13+
<form
14+
onSubmit={(event) => {
15+
// Stops the form from submitting to a URL
16+
// (the behavior which shows the ? in the URL and refreshes the page)
17+
event.preventDefault();
18+
19+
// Do some additional work, based on the form data
20+
setReversedUsername(username.split('').reverse().join(''));
21+
}}
22+
>
23+
<label>
24+
Username
25+
<input
26+
// 2. Use state variable
27+
value={username}
28+
onChange={(event) => {
29+
// 3. Update the state variable value
30+
setUsername(event.currentTarget.value);
31+
// another option (may not always work): event.target.value
32+
}}
33+
// // This is the name sent via the form, if we do not prevent the default action
34+
// name="username"
35+
/>
36+
</label>
37+
<button>Show reversed username</button>
38+
</form>
39+
40+
<div>Reversed username: {reversedUsername}</div>
41+
</div>
42+
);
43+
}

src/ExampleLiftingStateUp.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useState } from 'react';
2+
3+
export default function ExampleLiftingStateUp() {
4+
// 2. State was "lifted up" to here
5+
const [isOn, setIsOn] = useState(false);
6+
7+
return (
8+
<div>
9+
<h1>ExampleLiftingStateUp</h1>
10+
{/* 3. We added the `isOn` and `setIsOn` props to both components */}
11+
<LightBulb isOn={isOn} />
12+
<LightSwitch isOn={isOn} setIsOn={setIsOn} />
13+
</div>
14+
);
15+
}
16+
17+
// 4. Add `props` parameter to accept props
18+
function LightBulb(props) {
19+
// 1. Previously, the state was here
20+
// const [isOn, setIsOn] = useState(false);
21+
22+
// 5. Use props within component
23+
return <div>Lightbulb is {props.isOn ? '🌞 ON' : '🌑 OFF'}</div>;
24+
}
25+
26+
function LightSwitch(props) {
27+
return <button onClick={() => props.setIsOn(!props.isOn)}>Toggle</button>;
28+
}

src/ExampleStyling.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import styles from './ExampleStyling.module.scss';
2+
3+
export default function ExampleStyling() {
4+
return (
5+
<div>
6+
<h1>ExampleStyling</h1>
7+
<div
8+
style={{
9+
margin: '4px',
10+
padding: '8px',
11+
border: '4px solid orange',
12+
borderRadius: '8px',
13+
fontSize: '24px',
14+
}}
15+
>
16+
Tiger
17+
</div>
18+
<div className={styles.alligator}>Alligator</div>
19+
</div>
20+
);
21+
}

src/ExampleStyling.module.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.alligator {
2+
margin: 10px;
3+
padding: 12px;
4+
border: 3px solid green;
5+
border-radius: 8px;
6+
font-size: 24px;
7+
}

0 commit comments

Comments
 (0)