diff --git a/demo-app/package.json b/demo-app/package.json index 17ed638c0..4003e82fa 100644 --- a/demo-app/package.json +++ b/demo-app/package.json @@ -9,6 +9,7 @@ }, "devDependencies": { "@babel/core": "^7.16.7", + "@babel/plugin-transform-runtime": "^7.25.9", "@babel/preset-env": "^7.16.7", "@babel/preset-react": "^7.16.7", "@types/express": "^4.17.13", @@ -17,6 +18,7 @@ "@types/react-dom": "^17.0.19", "babel-loader": "^8.2.3", "copy-webpack-plugin": "^10.2.0", + "core-js": "^3.39.0", "css-loader": "^6.5.1", "html-webpack-plugin": "^5.5.0", "node": "^16.0.0", diff --git a/demo-app/src/client/Components/Buttons.tsx b/demo-app/src/client/Components/Buttons.tsx index b27abfe24..60235aa92 100644 --- a/demo-app/src/client/Components/Buttons.tsx +++ b/demo-app/src/client/Components/Buttons.tsx @@ -1,22 +1,82 @@ -import React from 'react'; -import Increment from './Increment'; +import React, { Component, useState } from 'react'; -function Buttons(): JSX.Element { - const buttons = []; - for (let i = 0; i < 4; i++) { - buttons.push(); +type ButtonProps = { + id: string; + label: string; + color?: string; + initialCount?: number; +}; + +type IncrementClassState = { + count: number; +}; + +class IncrementClass extends Component { + state = { + count: this.props.initialCount || 0, + }; + + handleClick = (): void => { + this.setState((prevState: IncrementClassState) => ({ + count: prevState.count + 1, + })); + }; + + render(): JSX.Element { + return ( +
+ +
+ ); } +} + +const IncrementFunction = (props: ButtonProps): JSX.Element => { + const [count, setCount] = useState(props.initialCount || 0); + + const handleClick = (): void => { + setCount((prev) => prev + 1); + }; return ( -
-

Stateful Buttons

-

- These buttons are functional components that each manage their own state with the useState - hook. -

- {buttons} +
+
); +}; + +class Buttons extends Component { + render(): JSX.Element { + return ( +
+

Mixed State Counter

+

First two buttons use class components, last two use function components.

+ + + + +
+ ); + } } export default Buttons; diff --git a/demo-app/src/client/Components/FunctionalReducerCounter.tsx b/demo-app/src/client/Components/FunctionalReducerCounter.tsx new file mode 100644 index 000000000..72af7ce26 --- /dev/null +++ b/demo-app/src/client/Components/FunctionalReducerCounter.tsx @@ -0,0 +1,124 @@ +import React, { useState, useReducer } from 'react'; + +type CounterProps = { + initialCount?: number; + step?: number; + title?: string; + theme?: { + backgroundColor?: string; + textColor?: string; + }; +}; + +type CounterState = { + count: number; + history: number[]; + lastAction: string; +}; + +type CounterAction = + | { type: 'INCREMENT' } + | { type: 'DECREMENT' } + | { type: 'DOUBLE' } + | { type: 'RESET' } + | { type: 'ADD'; payload: number }; + +function counterReducer(state: CounterState, action: CounterAction, step: number): CounterState { + switch (action.type) { + case 'INCREMENT': + return { + ...state, + count: state.count + step, + history: [...state.history, state.count + step], + lastAction: 'INCREMENT', + }; + case 'DECREMENT': + return { + ...state, + count: state.count - step, + history: [...state.history, state.count - step], + lastAction: 'DECREMENT', + }; + case 'DOUBLE': + return { + ...state, + count: state.count * 2, + history: [...state.history, state.count * 2], + lastAction: 'DOUBLE', + }; + case 'RESET': + return { + count: 0, + history: [], + lastAction: 'RESET', + }; + case 'ADD': + return { + ...state, + count: state.count + action.payload, + history: [...state.history, state.count + action.payload], + lastAction: `ADD ${action.payload}`, + }; + default: + return state; + } +} + +function FunctionalReducerCounter({ + initialCount = 0, + step = 1, + title = 'Function-based Reducer Counter', + theme = { + backgroundColor: '#ffffff', + textColor: '#330002', + }, +}: CounterProps): JSX.Element { + const [clickCount, setClickCount] = useState(0); + const [lastClickTime, setLastClickTime] = useState(null); + const [averageTimeBetweenClicks, setAverageTimeBetweenClicks] = useState(0); + const [state, dispatch] = useReducer( + (state: CounterState, action: CounterAction) => counterReducer(state, action, step), + { + count: initialCount, + history: [], + lastAction: 'none', + }, + ); + + return ( +
+

{title}

+
+

Current Count: {state.count}

+
+ +
+ + + + + +
+ +
+

Last Action: {state.lastAction}

+

History:

+
+ {state.history.map((value, index) => ( + + {value} + {index < state.history.length - 1 ? ' → ' : ''} + + ))} +
+
+
+ ); +} +export default FunctionalReducerCounter; diff --git a/demo-app/src/client/Components/FunctionalStateCounter.tsx b/demo-app/src/client/Components/FunctionalStateCounter.tsx new file mode 100644 index 000000000..8d942a369 --- /dev/null +++ b/demo-app/src/client/Components/FunctionalStateCounter.tsx @@ -0,0 +1,98 @@ +import React, { useState } from 'react'; + +type CounterProps = { + initialCount?: number; + step?: number; + title?: string; + theme?: { + backgroundColor?: string; + textColor?: string; + }; +}; + +function FunctionalStateCounter({ + initialCount = 0, + step = 1, + title = 'Function-based State Counter', + theme = { + backgroundColor: '#ffffff', + textColor: '#330002', + }, +}: CounterProps): JSX.Element { + const [count, setCount] = useState(initialCount); + const [history, setHistory] = useState([]); + const [lastAction, setLastAction] = useState('none'); + + const handleAction = (type: string, payload?: number) => { + let newCount = count; + switch (type) { + case 'INCREMENT': + newCount = count + step; + setCount(newCount); + setHistory([...history, newCount]); + setLastAction('INCREMENT'); + break; + case 'DECREMENT': + newCount = count - step; + setCount(newCount); + setHistory([...history, newCount]); + setLastAction('DECREMENT'); + break; + case 'DOUBLE': + newCount = count * 2; + setCount(newCount); + setHistory([...history, newCount]); + setLastAction('DOUBLE'); + break; + case 'ADD': + newCount = count + (payload || 0); + setCount(newCount); + setHistory([...history, newCount]); + setLastAction(`ADD ${payload}`); + break; + case 'RESET': + setCount(0); + setHistory([]); + setLastAction('RESET'); + break; + } + }; + + return ( +
+

{title}

+
+

Current Count: {count}

+
+ +
+ + + + + +
+ +
+

Last Action: {lastAction}

+

History:

+
+ {history.map((value, index) => ( + + {value} + {index < history.length - 1 ? ' → ' : ''} + + ))} +
+
+
+ ); +} + +export default FunctionalStateCounter; diff --git a/demo-app/src/client/Components/Increment.tsx b/demo-app/src/client/Components/Increment.tsx deleted file mode 100644 index 3836152a7..000000000 --- a/demo-app/src/client/Components/Increment.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import React, { useState } from 'react'; -function Increment(): JSX.Element { - const [count, setCount] = useState(0); - return ( -
- -
- ); -} - -export default Increment; diff --git a/demo-app/src/client/Components/Nav.tsx b/demo-app/src/client/Components/Nav.tsx index c289f8d81..53e12d553 100644 --- a/demo-app/src/client/Components/Nav.tsx +++ b/demo-app/src/client/Components/Nav.tsx @@ -11,7 +11,10 @@ function Nav(): JSX.Element { Tic-Tac-Toe - Counter + State Counter + + + Reducer Counter
); diff --git a/demo-app/src/client/Components/ReducerCounter.tsx b/demo-app/src/client/Components/ReducerCounter.tsx new file mode 100644 index 000000000..ae538b593 --- /dev/null +++ b/demo-app/src/client/Components/ReducerCounter.tsx @@ -0,0 +1,142 @@ +import React, { Component } from 'react'; + +type CounterProps = { + initialCount?: number; + step?: number; + title?: string; + theme?: { + backgroundColor?: string; + textColor?: string; + }; +}; + +type CounterState = { + count: number; + history: number[]; + lastAction: string; +}; + +type CounterAction = + | { type: 'INCREMENT' } + | { type: 'DECREMENT' } + | { type: 'DOUBLE' } + | { type: 'RESET' } + | { type: 'ADD'; payload: number }; + +class ReducerCounter extends Component { + static defaultProps = { + initialCount: 0, + step: 1, + title: 'Class-based Reducer Counter', + theme: { + backgroundColor: '#ffffff', + textColor: '#330002', + }, + }; + + static initialState(initialCount: number): CounterState { + return { + count: initialCount, + history: [], + lastAction: 'none', + }; + } + + static reducer(state: CounterState, action: CounterAction, step: number): CounterState { + switch (action.type) { + case 'INCREMENT': + return { + ...state, + count: state.count + step, + history: [...state.history, state.count + step], + lastAction: 'INCREMENT', + }; + case 'DECREMENT': + return { + ...state, + count: state.count - step, + history: [...state.history, state.count - step], + lastAction: 'DECREMENT', + }; + case 'DOUBLE': + return { + ...state, + count: state.count * 2, + history: [...state.history, state.count * 2], + lastAction: 'DOUBLE', + }; + case 'RESET': + return { + ...ReducerCounter.initialState(0), + lastAction: 'RESET', + }; + case 'ADD': + return { + ...state, + count: state.count + action.payload, + history: [...state.history, state.count + action.payload], + lastAction: `ADD ${action.payload}`, + }; + default: + return state; + } + } + + constructor(props: CounterProps) { + super(props); + this.state = ReducerCounter.initialState(props.initialCount || 0); + this.dispatch = this.dispatch.bind(this); + } + + dispatch(action: CounterAction): void { + this.setState((currentState) => + ReducerCounter.reducer(currentState, action, this.props.step || 1), + ); + } + + render(): JSX.Element { + const { title, theme } = this.props; + + return ( +
+

{title}

+
+

Current Count: {this.state.count}

+
+ +
+ + + + + +
+ +
+

Last Action: {this.state.lastAction}

+

History:

+
+ {this.state.history.map((value, index) => ( + + {value} + {index < this.state.history.length - 1 ? ' → ' : ''} + + ))} +
+
+
+ ); + } +} + +export default ReducerCounter; diff --git a/demo-app/src/client/Router.tsx b/demo-app/src/client/Router.tsx index 47dc6e14b..5ffbc1a55 100644 --- a/demo-app/src/client/Router.tsx +++ b/demo-app/src/client/Router.tsx @@ -6,11 +6,40 @@ import Nav from './Components/Nav'; import Board from './Components/Board'; import Home from './Components/Home'; import Buttons from './Components/Buttons'; +import ReducerCounter from './Components/ReducerCounter'; +import FunctionalReducerCounter from './Components/FunctionalReducerCounter'; // import ButtonsWithMoreHooks from './Components/ButtonsWithMoreHooks'; +import FunctionalStateCounter from './Components/FunctionalStateCounter'; const domNode = document.getElementById('root'); const root = createRoot(domNode); +const CounterPage = () => ( +
+ + + +
+); + root.render(