This commit is contained in:
Philippe Torrel
2026-08-26 09:39:22 +02:00
parent 5ae06e8825
commit c781b8c0bf
6 changed files with 25 additions and 14 deletions

View File

@@ -28,12 +28,16 @@ function App(props) {
<div className="container py-5"> <div className="container py-5">
<ExampleLazy /> <ExampleLazy />
<hr /> <hr />
<h3>useCallback mit ref- Attribut</h3>
<ExampleUseCallbackRef /> <ExampleUseCallbackRef />
{/* <hr />
<ExampleUseRef /> */}
<hr /> <hr />
<h3>useRef - Hook</h3>
<ExampleUseRef />
<hr />
<h3>useCallback auf EventHandler angewandt</h3>
<ExampleUseCallback /> <ExampleUseCallback />
<hr /> <hr />
<h3>memo - Funktion von React</h3>
<ExampleMemoParent /> <ExampleMemoParent />
<hr /> <hr />
<ExampleUseMemo /> <ExampleUseMemo />

View File

@@ -8,6 +8,7 @@ const ExampleUseCallback = (props) => {
{ id: 1, title: 'Product 1', description: 'lorem ipsum', price: 10 }, { id: 1, title: 'Product 1', description: 'lorem ipsum', price: 10 },
{ id: 2, title: 'Product 2', description: 'ipsum lorem', price: 20 }, { id: 2, title: 'Product 2', description: 'ipsum lorem', price: 20 },
{ id: 3, title: 'Product 3', description: 'lorem dolor', price: 30 }, { id: 3, title: 'Product 3', description: 'lorem dolor', price: 30 },
{ id: 4, title: 'Witcher 3', description: 'free update', price: 0 },
], ],
); );
@@ -47,7 +48,7 @@ const ExampleUseCallback = (props) => {
<ul className="list-group list-group-flush list-products"> <ul className="list-group list-group-flush list-products">
{products.map((product) => ( {products.map((product) => (
<ListItemProduct <ListItemProduct
key={`product-${product.id}`} key={`list-item-product-${product.id}`}
{...product} {...product}
onClick={(e) => { onClick={(e) => {
handleClick(e, product.id); handleClick(e, product.id);

View File

@@ -13,7 +13,7 @@ const ExampleUseMemo = (props) => {
const productsAmount = useMemo(() => { const productsAmount = useMemo(() => {
console.log('Calculating the number of products'); console.log('Calculating the number of products');
return products.length; return products.length;
}, [products]); }, [products]); // Bei Veränderung des "states" (hier: products) wird der Funktionsausdruck neu ausgeführt und der Rückgabewert in die Variable geschrieben
// EventHandler // EventHandler
const handleChangeText = (e) => { const handleChangeText = (e) => {
@@ -21,10 +21,10 @@ const ExampleUseMemo = (props) => {
}; };
// Functions // Functions
// const getNumberProducts = () => { const getNumberProducts = () => {
// console.log('Calculating the number of products'); console.log('Calculating the number of products');
// return products.length; return products.length;
// }; };
return ( return (
<div className="example-use-memo"> <div className="example-use-memo">

View File

@@ -1,13 +1,19 @@
import { useState } from 'react';
const LazyComponent = (props) => { const LazyComponent = (props) => {
// const { } = props; // const { } = props;
const [textLoaded, setTextLoaded] = useState('');
// Simulate a slow content placement // TODO: replace with fetch component // Simulate a slow content placement // TODO: replace with fetch component
const wait = (delay) => new Promise((resolve) => setTimeout(resolve, delay)); const wait = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
wait(3000); wait(3000).then(() => {
setTextLoaded('Content fully loaded');
});
return ( return (
<div className="lazy alert alert-info"> <div className="lazy alert alert-info">
<h4>I was loaded late!</h4> <h4>I was loaded late: {textLoaded}</h4>
<p>This text only appears after the code for this component has been loaded</p> <p>This text only appears after the code for this component has been loaded</p>
</div> </div>
); );

View File

@@ -16,9 +16,9 @@
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^19.2.18", "@types/react": "^19.2.18",
"@types/react-dom": "^19.2.4", "@types/react-dom": "^19.2.5",
"@vitejs/plugin-react": "^6.1.0", "@vitejs/plugin-react": "^6.1.0",
"oxlint": "^1.79.0", "oxlint": "^1.80.0",
"sass-embedded": "^1.103.1", "sass-embedded": "^1.103.1",
"vite": "^8.2.2" "vite": "^8.2.2"
} }

View File

@@ -18,9 +18,9 @@
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^19.2.18", "@types/react": "^19.2.18",
"@types/react-dom": "^19.2.4", "@types/react-dom": "^19.2.5",
"@vitejs/plugin-react": "^6.1.0", "@vitejs/plugin-react": "^6.1.0",
"oxlint": "^1.79.0", "oxlint": "^1.80.0",
"sass-embedded": "^1.103.1", "sass-embedded": "^1.103.1",
"vite": "^8.2.2" "vite": "^8.2.2"
} }