From bde137305014ddb23149ad9169824c4c20bc735b Mon Sep 17 00:00:00 2001 From: Amit Raikwar Date: Tue, 5 Dec 2023 17:54:22 +0530 Subject: [PATCH] 2. Showcase the working of React useReducer hook. - Managing two states simultaneously. - Managing three states simultaneously. --- src/App.tsx | 9 +- src/hooks/usereducer/UseReducerHook.tsx | 35 ++++++ .../usereducer/component/ButtonComponent.tsx | 86 +++++++++++++++ .../usereducer/component/SliderComponent.tsx | 102 ++++++++++++++++++ 4 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 src/hooks/usereducer/UseReducerHook.tsx create mode 100644 src/hooks/usereducer/component/ButtonComponent.tsx create mode 100644 src/hooks/usereducer/component/SliderComponent.tsx diff --git a/src/App.tsx b/src/App.tsx index 5349b8e..1876077 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,10 +1,11 @@ -import logo from './logo.svg'; -import './App.css'; +import logo from "./logo.svg"; +import "./App.css"; +import ReactUseReducerHooks from "./hooks/usereducer/UseReducerHook"; function App() { return ( -
- +
+
); } diff --git a/src/hooks/usereducer/UseReducerHook.tsx b/src/hooks/usereducer/UseReducerHook.tsx new file mode 100644 index 0000000..9566379 --- /dev/null +++ b/src/hooks/usereducer/UseReducerHook.tsx @@ -0,0 +1,35 @@ +import ButtonComponent from "./component/ButtonComponent"; +import SliderComponent from "./component/SliderComponent"; + +export interface ReactUseStateHooksProps {} + +export default function ReactUseReducerHooks(props: ReactUseStateHooksProps) { + return ( +
+ + +
+ ); +} + +function Row1() { + return ( + <> +
+ 1. Button component with useReducer hook +
+ + + ); +} + +function Row2() { + return ( + <> +
+ 2. Slider component with useReducer hook for three component states +
+ + + ); +} diff --git a/src/hooks/usereducer/component/ButtonComponent.tsx b/src/hooks/usereducer/component/ButtonComponent.tsx new file mode 100644 index 0000000..4f999c2 --- /dev/null +++ b/src/hooks/usereducer/component/ButtonComponent.tsx @@ -0,0 +1,86 @@ +import { Button } from "@mui/material"; +import { useReducer } from "react"; + +export interface IButtonComponentProps {} + +/** + * Enum definition for the action type for the component states. + */ +enum Type { + INCREMENT = "increment", + TOGGLE = "toggle", +} + +/** + * Type definition of the action type of the action for component. + */ +interface ActionType { + type: Type; +} + +/** + * Type definition of the total number of the states to manage. + */ +interface State { + count: number; + visible: boolean; +} + +/** + * Variable declaration for initial state of the component. + */ +const initialState: State = { + count: 0, + visible: false, +}; + +/** + * Reducer function to manage the states and actions. + * + * @param state Total number of the states to manage. + * @param action Action related to the state we have to manage. + * @returns + */ +function reducerFunction(state: State, action: ActionType) { + switch (action.type) { + case Type.INCREMENT: + return { count: state.count + 1, visible: state.visible }; + case Type.TOGGLE: + return { count: state.count, visible: !state.visible }; + } +} + +/** + * Function ui component for the button component. + * + * @param props Button component properies. + * @returns + */ +export default function ButtonComponent(props: IButtonComponentProps) { + const [state, dispatch] = useReducer(reducerFunction, initialState); + + const handleClick = () => { + dispatch({ type: Type.INCREMENT }); + dispatch({ type: Type.TOGGLE }); + }; + + return ( + <> +
+
+ +
+
+ Counter: {state.count} +
+ {state.visible && ( +
+ Hello I am now visible. +
+ )} +
+ + ); +} diff --git a/src/hooks/usereducer/component/SliderComponent.tsx b/src/hooks/usereducer/component/SliderComponent.tsx new file mode 100644 index 0000000..3677e1a --- /dev/null +++ b/src/hooks/usereducer/component/SliderComponent.tsx @@ -0,0 +1,102 @@ +import { Button, CircularProgress, Slider } from "@mui/material"; +import { useReducer } from "react"; + +export interface IButtonComponentProps {} + +/** + * Enum definition for the action type for the component states. + */ +enum Type { + CHANGE1 = "change1", + INCREMENT = "increment", + CHANGE2 = "change1", +} + +/** + * Type definition of the action type of the action for component. + */ +interface ActionType { + type: Type; + data: number; +} + +/** + * Type definition of the total number of the states to manage. + */ +interface State { + slide1: number; + count: number; + slide2: number; +} + +/** + * Variable declaration for initial state of the component. + */ +const initialState: State = { + slide1: 0, + count: 0, + slide2: 0, +}; + +/** + * Reducer function to manage the states and actions. + * + * @param state Total number of the states to manage. + * @param action Action related to the state we have to manage. + * @returns + */ +function reducerFunction(state: State, action: ActionType) { + switch (action.type) { + case Type.CHANGE1: + return { + slide1: action.data, + count: action.data, + slide2: action.data, + }; + case Type.INCREMENT: + return { + slide1: state.slide1, + count: state.count, + slide2: state.slide2, + }; + case Type.CHANGE2: + return { + slide1: action.data, + count: action.data, + slide2: action.data, + }; + } +} + +/** + * Function ui component for the button component. + * + * @param props Button component properies. + * @returns + */ +export default function ButtonComponent(props: IButtonComponentProps) { + const [state, dispatch] = useReducer(reducerFunction, initialState); + + const handleSlidingEvent = (e: any) => { + dispatch({ type: Type.CHANGE1, data: e.target.value }); + dispatch({ type: Type.CHANGE2, data: e.target.value }); + dispatch({ type: Type.INCREMENT, data: e.target.value }); + }; + + return ( + <> +
+
+ +
+
+
Circular progress
+ +
+
+ +
+
+ + ); +}