This commit is contained in:
Philippe Torrel
2026-10-26 11:38:50 +01:00
parent 688ad6e2cf
commit a1a85e4743
@@ -9,13 +9,17 @@ const initialState = {
// Reducer Funktion verwaltet das state Objekt. Über diese Funktion lässt sich das "komplexere" state-Objekt gezielt verändern. // Reducer Funktion verwaltet das state Objekt. Über diese Funktion lässt sich das "komplexere" state-Objekt gezielt verändern.
function reducer(state, action) { function reducer(state, action) {
// action Objekt hat immer folgende Eigenschaften: {type: 'ACTION_NAME', payload: changeValue } // action Objekt hat immer folgende Eigenschaften: {type: 'ACTION_NAME', payload: changeValue }
const { type } = action; const { type, payload = 1 } = action;
switch (type) { switch (type) {
case 'INCREASE': case 'INCREASE':
return { ...state, counter: state.counter + 1 }; return { ...state, counter: state.counter + 1 };
case 'DECREASE': case 'DECREASE':
return { ...state, counter: state.counter - 1 }; return { ...state, counter: state.counter - 1 };
case 'DOUBLE':
return { ...state, counter: state.counter * 2 };
case 'INC_VALUE':
return { ...state, counter: state.counter + payload };
default: default:
console.warn('Action Type not found'); console.warn('Action Type not found');
return state; return state;
@@ -39,10 +43,17 @@ const ExampleUseReducer = (props) => {
case 'decrease': case 'decrease':
dispatch({ type: 'DECREASE' }); dispatch({ type: 'DECREASE' });
break; break;
case 'double':
dispatch({ type: 'DOUBLE' });
break;
default: //nothing default: //nothing
} }
}; };
const handleClickIncValue = (e) => {
dispatch({ type: 'INC_VALUE', payload: 5 });
};
return ( return (
<div className="example-use-state"> <div className="example-use-state">
Counter: {state.counter} Counter: {state.counter}
@@ -52,6 +63,13 @@ const ExampleUseReducer = (props) => {
<button className="btn btn-dark button-dec mx-1" aria-label="Decrease" onClick={handleClick}> <button className="btn btn-dark button-dec mx-1" aria-label="Decrease" onClick={handleClick}>
<FaMinus /> <FaMinus />
</button> </button>
<button className="btn btn-dark button-double mx-1" aria-label="Double" onClick={handleClick}>
<FaPlus />
<FaPlus />
</button>
<button className="btn btn-dark button-double mx-1" aria-label="Increase by value" onClick={handleClickIncValue}>
<FaPlus /> 5
</button>
</div> </div>
); );
}; };