Skip to content

Commit

Permalink
feat: add set behaviour with useEffect but it trigger 2 component render
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Gerard committed Jan 20, 2024
1 parent 7dfc234 commit 6d3c310
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/components/Counter/Counter.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, memo, useCallback, useMemo } from 'react';
import {useState, memo, useCallback, useMemo, useEffect} from 'react';

import IconButton from '../UI/IconButton.jsx';
import MinusIcon from '../UI/Icons/MinusIcon.jsx';
Expand Down Expand Up @@ -33,6 +33,9 @@ const Counter = memo(function Counter({ initialCount }) {
[initialCount]
);

useEffect(() => {
setCounterChanges([{ value: initialCount, id: Math.random() * 1000}]);
}, [initialCount]);
// const [counter, setCounter] = useState(initialCount);
const [counterChanges, setCounterChanges] = useState([
{value: initialCount, id: Math.random() * 1000}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Counter/CounterHistory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function CounterHistory({ history }) {

return (
<ol>
{history.map((count) => (
{history.map((count, index) => (
<HistoryItem key={count.id} count={count.value} />
))}
</ol>
Expand Down

0 comments on commit 6d3c310

Please sign in to comment.