Skip to content

Commit 96d749f

Browse files
committed
both useState and useReducer states rendering on tool tip display but state includes reducer states as well
1 parent 9f2b5af commit 96d749f

File tree

5 files changed

+125
-6
lines changed

5 files changed

+125
-6
lines changed

demo-app/src/client/Components/FunctionalReducerCounter.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useReducer } from 'react';
1+
import React, { useState, useReducer } from 'react';
22

33
type CounterProps = {
44
initialCount?: number;
@@ -73,6 +73,9 @@ function FunctionalReducerCounter({
7373
textColor: '#330002',
7474
},
7575
}: CounterProps): JSX.Element {
76+
const [clickCount, setClickCount] = useState(0);
77+
const [lastClickTime, setLastClickTime] = useState<Date | null>(null);
78+
const [averageTimeBetweenClicks, setAverageTimeBetweenClicks] = useState<number>(0);
7679
const [state, dispatch] = useReducer(
7780
(state: CounterState, action: CounterAction) => counterReducer(state, action, step),
7881
{
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import React, { useState } from 'react';
2+
3+
type CounterProps = {
4+
initialCount?: number;
5+
step?: number;
6+
title?: string;
7+
theme?: {
8+
backgroundColor?: string;
9+
textColor?: string;
10+
};
11+
};
12+
13+
function FunctionalStateCounter({
14+
initialCount = 0,
15+
step = 1,
16+
title = 'Function-based State Counter',
17+
theme = {
18+
backgroundColor: '#ffffff',
19+
textColor: '#330002',
20+
},
21+
}: CounterProps): JSX.Element {
22+
const [count, setCount] = useState(initialCount);
23+
const [history, setHistory] = useState<number[]>([]);
24+
const [lastAction, setLastAction] = useState('none');
25+
26+
const handleAction = (type: string, payload?: number) => {
27+
let newCount = count;
28+
switch (type) {
29+
case 'INCREMENT':
30+
newCount = count + step;
31+
setCount(newCount);
32+
setHistory([...history, newCount]);
33+
setLastAction('INCREMENT');
34+
break;
35+
case 'DECREMENT':
36+
newCount = count - step;
37+
setCount(newCount);
38+
setHistory([...history, newCount]);
39+
setLastAction('DECREMENT');
40+
break;
41+
case 'DOUBLE':
42+
newCount = count * 2;
43+
setCount(newCount);
44+
setHistory([...history, newCount]);
45+
setLastAction('DOUBLE');
46+
break;
47+
case 'ADD':
48+
newCount = count + (payload || 0);
49+
setCount(newCount);
50+
setHistory([...history, newCount]);
51+
setLastAction(`ADD ${payload}`);
52+
break;
53+
case 'RESET':
54+
setCount(0);
55+
setHistory([]);
56+
setLastAction('RESET');
57+
break;
58+
}
59+
};
60+
61+
return (
62+
<div
63+
className='reducer-counter'
64+
style={{
65+
backgroundColor: theme.backgroundColor,
66+
color: theme.textColor,
67+
}}
68+
>
69+
<h2>{title}</h2>
70+
<div className='counter-value'>
71+
<h3>Current Count: {count}</h3>
72+
</div>
73+
74+
<div className='counter-buttons'>
75+
<button onClick={() => handleAction('INCREMENT')}>Increment (+{step})</button>
76+
<button onClick={() => handleAction('DECREMENT')}>Decrement (-{step})</button>
77+
<button onClick={() => handleAction('DOUBLE')}>Double Value</button>
78+
<button onClick={() => handleAction('ADD', 5)}>Add 5</button>
79+
<button onClick={() => handleAction('RESET')}>Reset</button>
80+
</div>
81+
82+
<div className='counter-info'>
83+
<h4>Last Action: {lastAction}</h4>
84+
<h4>History:</h4>
85+
<div className='history-list'>
86+
{history.map((value, index) => (
87+
<span key={index}>
88+
{value}
89+
{index < history.length - 1 ? ' → ' : ''}
90+
</span>
91+
))}
92+
</div>
93+
</div>
94+
</div>
95+
);
96+
}
97+
98+
export default FunctionalStateCounter;

demo-app/src/client/Router.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,34 @@ import Buttons from './Components/Buttons';
99
import ReducerCounter from './Components/ReducerCounter';
1010
import FunctionalReducerCounter from './Components/FunctionalReducerCounter';
1111
// import ButtonsWithMoreHooks from './Components/ButtonsWithMoreHooks';
12+
import FunctionalStateCounter from './Components/FunctionalStateCounter';
1213

1314
const domNode = document.getElementById('root');
1415
const root = createRoot(domNode);
1516

1617
const CounterPage = () => (
1718
<div style={{ display: 'flex', justifyContent: 'center', gap: '2rem', flexWrap: 'wrap' }}>
18-
<ReducerCounter key='ReducerCounter' />
19-
<FunctionalReducerCounter key='FunctionalReducerCounter' />
19+
<ReducerCounter
20+
key='ReducerCounter'
21+
initialCount={5}
22+
step={2}
23+
title='Class Counter'
24+
theme={{ backgroundColor: '#f5f5f5', textColor: '#2a2a2a' }}
25+
/>
26+
<FunctionalReducerCounter
27+
key='FunctionalReducerCounter'
28+
initialCount={10}
29+
step={3}
30+
title='Functional Reducer'
31+
theme={{ backgroundColor: '#e8f4f8', textColor: '#2b4d59' }}
32+
/>
33+
<FunctionalStateCounter
34+
key='FunctionalStateCounter'
35+
initialCount={15}
36+
step={5}
37+
title='Functional State'
38+
theme={{ backgroundColor: '#f8f4e8', textColor: '#594b2b' }}
39+
/>
2040
</div>
2141
);
2242

src/backend/controllers/createTree.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ export default function createTree(currentFiberNode: Fiber): Tree {
147147
// Else filter & format props data to ensure they are JSON stringify-able, before sending to front end
148148
default: {
149149
componentData.props = filterAndFormatData(memoizedProps);
150-
console.log('Default case props for', componentName, ':', componentData.props);
151150
}
152151
}
153152
}
@@ -210,7 +209,6 @@ export default function createTree(currentFiberNode: Fiber): Tree {
210209
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
211210
console.log('hook states', hooksStates);
212211
// Get the hooks names by parsing the elementType
213-
console.log('element type', elementType.toString());
214212
const hooksNames = getHooksNames(elementType.toString());
215213
console.log('hook names', hooksNames);
216214

src/backend/controllers/statePropExtractors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function getHooksStateAndUpdateMethod(
8282
});
8383
}
8484
// Existing useState check
85-
else if (memoizedState.queue) {
85+
if (memoizedState.queue) {
8686
hooksStates.push({
8787
component: memoizedState.queue,
8888
state: memoizedState.memoizedState,

0 commit comments

Comments
 (0)