diff --git a/src/Button.css b/src/Button.css new file mode 100644 index 0000000..9403a3c --- /dev/null +++ b/src/Button.css @@ -0,0 +1,5 @@ +/* Button.css */ +.btn { + background-color: #38c78b; + border: 1px solid #1f6e4d; +} \ No newline at end of file diff --git a/src/Button.jsx b/src/Button.jsx new file mode 100644 index 0000000..63ec400 --- /dev/null +++ b/src/Button.jsx @@ -0,0 +1,14 @@ +import React from "react"; +import "./Button.css"; + +class Button extends React.Component { + render() { + return ( + + ); + } +} + +export default Button; diff --git a/src/CustomButton.jsx b/src/CustomButton.jsx new file mode 100644 index 0000000..1085f40 --- /dev/null +++ b/src/CustomButton.jsx @@ -0,0 +1,13 @@ +import React from "react"; + +class CustomButton extends React.Component { + render() { + return ( + + ); + } +} + +export default CustomButton; diff --git a/src/Greeting.jsx b/src/Greeting.jsx new file mode 100644 index 0000000..6d566a1 --- /dev/null +++ b/src/Greeting.jsx @@ -0,0 +1,16 @@ +import React from "react"; + +class Greeting extends React.Component { + render() { + return ( +
+

+ Hello {this.props.firstName} {this.props.lastName} +

+

Good to see you!

+
+ ); + } +} + +export default Greeting; diff --git a/src/GreetingImage.jsx b/src/GreetingImage.jsx new file mode 100644 index 0000000..12d81d3 --- /dev/null +++ b/src/GreetingImage.jsx @@ -0,0 +1,23 @@ +import React from "react"; + +/** + * props: + * name: string + * withImage: boolean + */ +class GreetingImage extends React.Component { + render() { + const { name, withImage } = this.props; + + return ( +
+

Good Morning {name}

+ {withImage && ( + avatar + )} +
+ ); + } +} + +export default GreetingImage; diff --git a/src/GreetingTime.jsx b/src/GreetingTime.jsx new file mode 100644 index 0000000..8c3205a --- /dev/null +++ b/src/GreetingTime.jsx @@ -0,0 +1,20 @@ +import React from "react"; + +/** + * props: + * name: string + * isMorning: boolean + */ +class GreetingTime extends React.Component { + render() { + const { name, isMorning } = this.props; + + return ( +
+ {isMorning ?

Good Morning {name}

:

Goodbye {name}

} +
+ ); + } +} + +export default GreetingTime; diff --git a/src/PersonList.jsx b/src/PersonList.jsx new file mode 100644 index 0000000..efa1fb6 --- /dev/null +++ b/src/PersonList.jsx @@ -0,0 +1,30 @@ +import React from "react"; + +export class PersonList extends React.Component { + /** + * Renders a list of persons. + * @note Using array indices as keys is generally not recommended because: + * 1. It can cause performance issues when the list order changes (additions/removals) + * 2. It may lead to unexpected behavior or bugs with component state + * 3. React specifically warns against this practice in its documentation + * 4. If the list is re-ordered, indices will change, causing unnecessary re-renders + * + * Ideally, use a unique and stable identifier from each item as the key instead. + * Like here we could use person.name as the key. + */ + render() { + const persons = [ + { name: "John Doe" }, + { name: "Georges Abitbol" }, + { name: "Kevin McGregor" }, + ]; + + return ( + + ); + } +} diff --git a/src/main.jsx b/src/main.jsx index 080c501..f829b38 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -1,32 +1,52 @@ import { createRoot } from "react-dom/client"; -const user = { - id: 1, - name: "John Doe", -}; +import Button from "./Button"; +import CustomButton from "./CustomButton"; +import Greeting from "./Greeting"; +import GreetingImage from "./GreetingImage"; +import GreetingTime from "./GreetingTime"; +import { PersonList } from "./PersonList"; -function capitalize(name) { - return name.toUpperCase(); +function handleClick() { + alert("Congratulation! You clicked a button!"); } -function getAvatarUrl(user) { - return `https://api.dicebear.com/9.x/adventurer/svg?seed=${user.name}${user.id}`; -} - -const greeting = ( +const greetings = (
-

Hello {capitalize(user.name)}

- {`${user.name}'s + + +
); -const header = ( -
- picsum - {greeting} -
+const customButton = ( + + Click me + ); -const root = createRoot(document.getElementById("root")); +const button = ; + +const greetingMorning = ; +const greetingEvening = ; -root.render(header); +const greetingWithImage = ; // +const greetingWithoutImage = ; + +const root = createRoot(document.getElementById("root")); +root.render( + <> + {greetings} + --- + {customButton} + {button} + --- + + --- + {greetingMorning} + {greetingEvening} + --- + {greetingWithImage} + {greetingWithoutImage} + , +);