From 3370aa2caa1f66df30ce27ee9fe4bdb164cd9d3e Mon Sep 17 00:00:00 2001 From: Philippe Torrel Date: Wed, 28 Oct 2026 11:37:43 +0100 Subject: [PATCH] --- .../components/examples/ExampleUseContext.jsx | 18 +++++ .../src/components/examples/ExampleUseId.jsx | 21 +++++ .../components/examples/ExampleUseReducer.jsx | 77 +++++++++++++++++++ .../components/examples/ExampleUseState.jsx | 36 +++++++++ .../examples/theme-switcher/ThemeContext.jsx | 8 ++ .../examples/theme-switcher/ThemeProvider.jsx | 25 ++++++ 6 files changed, 185 insertions(+) create mode 100644 06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseContext.jsx create mode 100644 06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseId.jsx create mode 100644 06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseReducer.jsx create mode 100644 06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseState.jsx create mode 100644 06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeContext.jsx create mode 100644 06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeProvider.jsx diff --git a/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseContext.jsx b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseContext.jsx new file mode 100644 index 0000000..66c0a02 --- /dev/null +++ b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseContext.jsx @@ -0,0 +1,18 @@ +import { useContext } from 'react'; +import ThemeContext from './theme-switcher/ThemeContext'; + +const ExampleUseContext = (props) => { + // const { } = props; + + const theme = useContext(ThemeContext); + + return ( +
+

useContext

+

+ The theme of this component is: {theme} +

+
+ ); +}; +export default ExampleUseContext; diff --git a/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseId.jsx b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseId.jsx new file mode 100644 index 0000000..90ec7f8 --- /dev/null +++ b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseId.jsx @@ -0,0 +1,21 @@ +import { useId } from 'react'; +// import { v4 as uid} from 'uuid'; +const ExampleUseId = (props) => { + // const { } = props; + + const id = useId(); // _r_1 + // const id = uid(); // von uuid ⇨ 'b18794e8-5d0d-417c-b361-ba38e78411b4' + + return ( +
+

+ ID: {id} +

+
+ + +
+
+ ); +}; +export default ExampleUseId; diff --git a/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseReducer.jsx b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseReducer.jsx new file mode 100644 index 0000000..d44cb1a --- /dev/null +++ b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseReducer.jsx @@ -0,0 +1,77 @@ +import { useReducer } from 'react'; +import { FaPlus, FaMinus } from 'react-icons/fa6'; + +const initialState = { + counter: 0, + // more props.... +}; + +// Reducer Funktion verwaltet das state Objekt. Über diese Funktion lässt sich das "komplexere" state-Objekt gezielt verändern. +function reducer(state, action) { + // action Objekt hat immer folgende Eigenschaften: {type: 'ACTION_NAME', payload: changeValue } + const { type, payload = 1 } = action; + + switch (type) { + case 'INCREASE': + return { ...state, counter: state.counter + 1 }; + + case 'DECREASE': + return { ...state, counter: state.counter - 1 }; + case 'DOUBLE': + return { ...state, counter: state.counter * 2 }; + case 'INC_VALUE': + return { ...state, counter: state.counter + payload }; + default: + console.warn('Action Type not found'); + return state; + } +} + +const ExampleUseReducer = (props) => { + // const { } = props; + + // Hooks - Top Level + // const [counter, setCounter] = useState(0); + const [state, dispatch] = useReducer(reducer, initialState); + + const handleClick = (e) => { + const btn = e.currentTarget; + const label = btn.ariaLabel; // btn.getAttribute('aria-label') + switch (label.toLowerCase()) { + case 'increase': + dispatch({ type: 'INCREASE' }); + break; + case 'decrease': + dispatch({ type: 'DECREASE' }); + break; + case 'double': + dispatch({ type: 'DOUBLE' }); + break; + default: //nothing + } + }; + + const handleClickIncValue = (e) => { + dispatch({ type: 'INC_VALUE', payload: 5 }); + }; + + return ( +
+ Counter: {state.counter} + + + + +
+ ); +}; +export default ExampleUseReducer; diff --git a/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseState.jsx b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseState.jsx new file mode 100644 index 0000000..1a93578 --- /dev/null +++ b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/ExampleUseState.jsx @@ -0,0 +1,36 @@ +import { useState } from 'react'; +import { FaPlus, FaMinus } from 'react-icons/fa6'; + +const ExampleUseState = (props) => { + // const { } = props; + const [counter, setCounter] = useState(0); + + const handleClick = (e) => { + const btn = e.currentTarget; + const label = btn.ariaLabel; // btn.getAttribute('aria-label') + switch (label.toLowerCase()) { + case 'increase': + setCounter(counter + 1); + break; + case 'decrease': + setCounter((prevState) => { + return prevState - 1; + }); + break; + default: //nothing + } + }; + + return ( +
+ Counter: {counter} + + +
+ ); +}; +export default ExampleUseState; diff --git a/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeContext.jsx b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeContext.jsx new file mode 100644 index 0000000..dda5e98 --- /dev/null +++ b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeContext.jsx @@ -0,0 +1,8 @@ +import { createContext } from 'react'; + +export const THEME_DEFAULT_VALUE = 'light'; + +const ThemeContext = createContext(THEME_DEFAULT_VALUE); +// createContext -> JSX-Element mit value Attribut +// ... +export default ThemeContext; diff --git a/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeProvider.jsx b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeProvider.jsx new file mode 100644 index 0000000..fa141c5 --- /dev/null +++ b/06_react/unterricht/tag38/02_react-api-with-hooks/src/components/examples/theme-switcher/ThemeProvider.jsx @@ -0,0 +1,25 @@ +import { useState } from 'react'; +import ThemeContext, { THEME_DEFAULT_VALUE } from './ThemeContext'; + +const ThemeProvider = (props) => { + const { children } = props; + + console.log(ThemeContext._currentValue); // 'light' <- _currentValue beinhaltet initialWert. Soll aber in production Umgebung nicht verwendet werden, da kein offizieller Standard. + + const [theme, setTheme] = useState(THEME_DEFAULT_VALUE); + + const handleToggleTheme = (e) => { + console.log('toggle theme'); + setTheme((prevState) => (prevState === 'light' ? 'dark' : 'light')); + }; + + return ( + + + {children} + + ); +}; +export default ThemeProvider;