Why React hook useState don't increment index onClick? - reactjs

I don't understand why in setProjectIndex inside SwitchProject function not updating my projectIndex state :
const WorkPreview = ({project, projects}) => {
const [currProject, setCurrProject] = useState(projects[0]);
const [projectIndex, setProjectIndex] = useState(0);
useEffect(() => {
console.log("useEffect idx", projectIndex) // log: 1 when onClick to Right Button
}, [projectIndex])
const SwitchProject = (incrDecrAmount) => {
let nextIndex = projectIndex + incrDecrAmount;
if (nextIndex >= 0 && nextIndex < (projects.length-1)) {
setProjectIndex(projectIndex + incrDecrAmount); // sets 0
console.log("projectIndex", projectIndex) // log: 0 when onClick to Right Button (increment by 1)
console.log("nextIdx", nextIndex) // log: 1 when onClick to Right Button
setCurrProject(projects[projectIndex]);
console.log("", projects[projectIndex]); // gives projects[0] not projects[1]
}
}
return (
<div className="works__preview" id="workPreview">
<button className="works__itemBtn" id="btnfixedLeft" onClick={() => { SwitchProject(-1) }}>
<Icon.ArrowLeft></Icon.ArrowLeft>
</button>
<button className="works__itemBtn" id="btnfixedRight" onClick={() => { SwitchProject(1) }}>
<Icon.ArrowRight></Icon.ArrowRight>
</button>
</div>
)
I've checked other questions and try different ways but gives the same result
Can someone explain me that and gives me the solution ?

Since you are just increasing/decreasing values, in your position I would just utilize a userReducer hook
import React, { useState, useEffect, useReducer } from "react";
const useCounterHook = (state: { count: number }, action: { type: string }) => {
switch (action.type) {
case "increment":
return { count: ++state.count };
case "decrement":
return { count: --state.count };
default:
throw new Error();
}
};
const WorkPreview = ({ project, projects }) => {
const [currProject, setCurrProject] = useState(projects[0]);
const [state, dispatch] = useReducer(useCounterHook, { count: 0 });
useEffect(() => {
console.log("state effect", state.count);
console.log("state next effect", state.count + 1);
setCurrProject(projects[state.count < 0 ? 0 : state.count]);
}, [projects, state]);
useEffect(() => {
console.log("currProject", currProject);
}, [currProject]);
return (
<div className="works__preview" id="workPreview">
<button
className="works__itemBtn"
id="btnfixedLeft"
onClick={() => dispatch({ type: "decrement" })}
>
<Icon.ArrowLeft></Icon.ArrowLeft>
</button>
<button
className="works__itemBtn"
id="btnfixedRight"
onClick={() => dispatch({ type: "increment" })}
>
<Icon.ArrowRight></Icon.ArrowRight>
</button>
</div>
);
};

Related

Why is the min state getting updated 2 times instead of only once?

Why is the min state getting updated in the multiples of two instead of just updating by one after every 59 seconds? How do I fix it?
import { useRef, useState } from "react";
export const Timer = () => {
const [second, setSecond] = useState(0);
const [min, setMin] = useState(0);
const watch = useRef(null);
const startTimer = () => {
watch.current = setInterval(() => {
setSecond((value) => {
if (value === 59) {
setSecond(0);
setMin((v) => v + 1);
}
return value + 1;
});
}, 1000);
};
return (
<div>
<h1>
{min}:{second}{" "}
</h1>
<button onClick={startTimer}>Start</button>
<button onClick={() => clearInterval(watch.current)}>Pause</button>
<button
onClick={() => {
setSecond(0);
return clearInterval(watch.current);
}}
>
Reset
</button>
</div>
);
};
This is the component as a whole. I am new to react so please help.

React : Maximum update depth exceeded

Still with my react project, now I'm learning the hooks, perhaps I have an issue with the 'infinite loop' (Maximum update depth exceeded) and I can't figure out how to handle this. I have redux to handle the states. I used useEffect, because when I clicked on a div, it was showing, or did what I wanted, always one step late
function OffersWhatTypeOfWebsiteComponent(props) {
const dispatch = useDispatch()
const [active, setActive] = useState(1)
const [typeWebSite, setTypeWebSite] = useState('withCMS')
const updateTypeWebSite = () => {
dispatch({
type: "TYPE_WEBSITE",
payload: typeWebSite
})
}
useEffect(() => {
updateTypeWebSite();
}, [updateTypeWebSite()]);
const renderElements = (props) => {
switch (active) {
case 1 :
return (
<>
<OffersChooseYourPackageCMSComponent
/>
</>
);
break
case 2 :
return (
<>
<OffersChooseYourPackageLikeAProComponent/>
</>
)
default:
return 'error'
}
}
return (
<div>
<OffersTitleCardComponent
titleNumber='2'
titleSection='What type of Website'
/>
<div className='chooseYourProject'>
<OffersCardsWithCheckComponent
titleCard='With CMS'
subtitleCard='xxxx'
active={active === 1 ? 'listing-active' : 'listing'}
onClick={() => {
setActive(1);
setTypeWebSite('withCMS');
updateTypeWebSite()
}}
/>
<OffersCardsWithCheckComponent
titleCard='Like a pro'
subtitleCard='xxx'
active={active === 2 ? 'listing-active' : 'listing'}
onClick={() => {
setActive(2);
setTypeWebSite('like a pro');
updateTypeWebSite()
}}
/>
</div>
{
renderElements({})
}
</div>
);
}
export default OffersWhatTypeOfWebsiteComponent;
This is the sub-component :
function OffersChooseYourPackageCMSComponent(props) {
const dispatch = useDispatch()
const [active, setActive] = useState(1)
const [packageWebSite, setPackageWebSite] = useState('Shopify')
const [pricePackageWebSite, setPricePackageWebSite] = useState(300)
const updatePackageWebSite = () => {
dispatch({
type: "PACKAGE_WEBSITE",
payload: {packageWebSite, pricePackageWebSite}
})
}
useEffect(() => {
updatePackageWebSite();
}, [updatePackageWebSite()]);
const renderElements = () => {
switch (active) {
case 1 :
return (
<>
<OffersNumbersOfProductsComponent/>
</>
);
break
case 2 :
return (
<>
<OffersNumbersOfPagesComponent/>
<OffersWoocommerceComponent/>
</>
);
break
default :
return 'error'
}
}
return (
<div>
<OffersTitleCardComponent
titleNumber='3'
titleSection='Choose your package'
/>
<div className="shopify">
<OffersCardsWithCheckComponent
onClick={() => {
setActive(1);
setPackageWebSite('Shopify');
setPricePackageWebSite(300);
updatePackageWebSite()
}}
active={active === 1 ? "listing-active" : "listing"}
titleCard='Shopify'
subtitleCard='xxx'
pricePackage='$54349'
detailPrice='(1 landing page + up to 5 products)'
/>
<OffersCardsWithCheckComponent
onClick={() => {
setActive(2);
setPackageWebSite('WordPress');
setPricePackageWebSite(900);
updatePackageWebSite()
}}
active={active === 2 ? "listing-active" : "listing"}
titleCard='WordPress'
subtitleCard='xxxx'
pricePackage='$23349'
detailPrice='(1 landing page)'
/>
</div>
{renderElements()}
</div>
);
}
export default OffersChooseYourPackageCMSComponent;
Don't hesitate to tell me some good practices too, on what I should arrange also if needed.
Thanks for your help
You should replicate this into your sub-component as well
const updateTypeWebSite = useCallback(() => {
dispatch({
type: "TYPE_WEBSITE",
payload: typeWebSite
})
}, [typeWebSite])
useEffect(() => updateTypeWebSite(), [updateTypeWebSite]);
Read this at reactjs documentation
Found something that worked, don't know if it's the best solutuion :
const [typeWebSite, setTypeWebSite] = useState('withCMS')
const updateTypeWebSite = () => {
dispatch({
type: "TYPE_WEBSITE",
payload: typeWebSite
})
}
useEffect(() => {
updateTypeWebSite()
},[typeWebSite,setTypeWebSite]);

How React manage state updates wrapped in async block

In this code i expect when i clicked asycn change button to have output:
num1: 6
num1: 7
as each state update in then() block so React does not batch the updates
but i got 2 lines:
num1: 6
import React, { useState } from "react";
import { Button } from "react-bootstrap";
const Test = (props) => {
const [num1, setNum1] = useState(5);
const [num2, setNum2] = useState(6);
const syncClickHandler = () => {
setNum1(num1 + 1);
setNum1(num1 + 1);
};
console.log("num1 " + num1);
const asyncClickHandler = () => {
Promise.resolve()
.then(() => {
setNum1(num1 + 1);
})
.then(() => {
setNum1(num1 + 1);
});
};
return (
<div>
num1: {num1} , num2: {num2} <br />
<Button onClick={syncClickHandler}>sync change</Button>
<Button onClick={asyncClickHandler}>async change</Button>
</div>
);
};
export default Test;
why i got this output
React does not batch state updates in async code (yet v17, may change in future).
Moreover, you have closure on num1 value (num1 === 6), so the second setNum call won't trigger additional render (prev value 7 === curr value 7).
Try using functional updates and discard the closure:
const asyncClickHandler = () => {
Promise.resolve()
.then(() => {
setNum((prev) => prev + 1);
})
.then(() => {
setNum((prev) => prev + 1);
});
};
A full example, notice the number of logs, sync set state are batched and async aren't.
const Test = () => {
const [num, setNum] = useState(0);
const syncClickHandler = () => {
setNum((prev) => prev + 1);
setNum((prev) => prev + 1);
};
const asyncClickHandler = () => {
Promise.resolve()
.then(() => {
setNum((prev) => prev + 1);
})
.then(() => {
setNum((prev) => prev + 1);
});
};
console.log("num " + num);
return (
<div>
num1: {num} <br />
<button onClick={syncClickHandler}>sync change</button>
<button onClick={asyncClickHandler}>async change</button>
</div>
);
};

How to reset the timer using UseEffect React Hooks

I'm currently trying to make a pomodoro timer. One feature I noticed on some timers is that if you click reset time, it'll go to the default time (usually 15 minutes).
I want it to be a little more advanced so that when you click reset, it will reset to whatever value you set the timer to.
For example, when you open the screen the default time is 15 minutes. If you add 5 minutes, then press start, then press reset, it should reset to 20 minutes. Instead, my code is resetting it to 15.
Can anyone think of a way to reset time?
Apologies if it's a bad explanation. Thank you in advance.
Here's my code so far using React Hooks:
import './App.css';
function App() {
const [session, setSession] = useState(5)
const [timer, setTimer] = useState(2)
const [isRunning, setIsRunning] = useState(false)
const [resetTime, setResetTime] = useState(900)
let time = new Date(timer * 1000).toISOString().substr(11, 8);
function sessionIncrement() {
setSession(prevSession => session + 1)
}
function sessionDecrement() {
if (session > 0) {
setIsRunning(false)
setSession(prevSession => prevSession > 0 && prevSession - 1)
}
}
function resetTimer() {
setIsRunning(false)
setTimer(resetTime)
}
function increment() {
setIsRunning(false);
setTimer(prevTimer => prevTimer + 300);
}
function decrement() {
setIsRunning(false);
setTimer(prevTimer => prevTimer - 300);
}
useEffect(() => {
if (isRunning) {
const interval = setInterval(() => {
setTimer(prevTimer => prevTimer > 0 && prevTimer - 1)
}, 1000);
if (timer === 0) {
sessionDecrement()
setIsRunning(false)
}
return () => clearInterval(interval)
}
}, [isRunning, session, timer])
useEffect(() => {
setResetTime(timer)
}, [])
return (
<div className="App">
<h1>Session #{session}</h1>
<button onClick={() => sessionDecrement()}>-</button>
<button onClick={() => sessionIncrement()}>+</button>
<h1>{time}</h1>
<button onClick={() => setIsRunning(false)} >Pause</button>
<button onClick={() => setIsRunning(true)}>Start</button>
<button onClick={() => resetTimer()}>Reset</button>
<button onClick={() => decrement()}>-</button>
<button onClick={() => increment()}>+</button>
</div>
);
}
export default App;
The first thing you'll want to do is change the timer and reset time to use the same value:
const initialTime = 900;
const [session, setSession] = useState(5);
const [timer, setTimer] = useState(initialTime);
const [isRunning, setIsRunning] = useState(false);
const [resetTime, setResetTime] = useState(initialTime);
Then, you'll want to change increment and decrement functions so that they change the reset value as well as the timer value:
function increment() {
setIsRunning(false);
setTimer((prevTimer) => prevTimer + 300);
setResetTime((prevResetTime) => prevResetTime + 300);
}
function decrement() {
setIsRunning(false);
setTimer((prevTimer) => prevTimer - 300);
setResetTime((prevResetTime) => prevResetTime - 300);
}
If I understand you right, you just need to set resetTime to the same value as timer, in the increment and decrement function. Like this:
import './App.css';
function App() {
const [session, setSession] = useState(5)
const [timer, setTimer] = useState(2)
const [isRunning, setIsRunning] = useState(false)
const [resetTime, setResetTime] = useState(900)
let time = new Date(timer * 1000).toISOString().substr(11, 8);
function sessionIncrement() {
setSession(prevSession => session + 1)
}
function sessionDecrement() {
if (session > 0) {
setIsRunning(false)
setSession(prevSession => prevSession > 0 && prevSession - 1)
}
}
function resetTimer() {
setIsRunning(false)
setTimer(resetTime)
}
function increment() {
const newTime = timer + 300
setIsRunning(false);
setTimer(newTime);
setResetTime(newTime)
}
function decrement() {
const newTime = timer - 300
setIsRunning(false);
setTimer(newTime);
setResetTime(newTime)
}
useEffect(() => {
if (isRunning) {
const interval = setInterval(() => {
setTimer(prevTimer => prevTimer > 0 && prevTimer - 1)
}, 1000);
if (timer === 0) {
sessionDecrement()
setIsRunning(false)
}
return () => clearInterval(interval)
}
}, [isRunning, session, timer])
useEffect(() => {
setResetTime(timer)
}, [])
return (
<div className="App">
<h1>Session #{session}</h1>
<button onClick={() => sessionDecrement()}>-</button>
<button onClick={() => sessionIncrement()}>+</button>
<h1>{time}</h1>
<button onClick={() => setIsRunning(false)} >Pause</button>
<button onClick={() => setIsRunning(true)}>Start</button>
<button onClick={() => resetTimer()}>Reset</button>
<button onClick={() => decrement()}>-</button>
<button onClick={() => increment()}>+</button>
</div>
);
}
export default App;

Line 0: Parsing error: Cannot read property 'map' of undefined" - TypeScript Custom Hook

I have got an error "./src/hooks/usePagination.tsx Line 0: Parsing error: Cannot read property 'map' of undefined" Do you know what is the reason? I spent half the night looking for the answer on stackoferflow etc and had many unsuccessful attempts to fix that.
First I thought about my data format, because now it is
[
{"id":1,"firstName":"Cori","lastName":"Wescott","email":"cwescott0#etsy.com","gender":"Male","ip_address":"236.58.118.85"},
{"id":2,"firstName":"Teena","lastName":"Kedge","email":"tkedge1#ameblo.jp","gender":"Female","ip_address":"74.32.179.20"},
{"id":3,"firstName":"Englebert","lastName":"Menlove","email":"emenlove2#cmu.edu","gender":"Male","ip_address":"91.249.51.126"},
]
and I changed data instead of json, but it didn't help. Later I have looked for a wrong mark, letter, character, whatever - no results. Finally, my research pushed me to reading about too old version of npm or other package. Eh I give up now..
Maybe somebody will find a problem.
function App() {
const [paginationState, paginationActions] = usePagination(dataEntries, 2);
return (
<div className="App">
<h2>Users</h2>
{ !paginationState.isBusy && <PaginatedTable dataEntries={paginationState.entries}/> }
<Pagination state={paginationState} actions={paginationActions}/>
</div>
);
}
export default App;
d.ts file
export interface User {
id: number
firstName: string
lastName: string
}
export interface PaginationState<T> {
lastPageIdx: number
actualPageIdx: number
entries: T[]
isBusy: boolean
}
export interface PaginationActions {
goToFirstPage: () => void
goToPrevPage: () => void
goToNextPage: () => void
goToLastPage: () => void
goToPage: (number: number) => void
}
custom hook
function usePagination<T>(dataEntries: T[], elementsOnPage: number): [PaginationState<T>, PaginationActions] {
const [actualPageIdx, setActualPageIdx] = useState(1)
const lastPageIdx = Math.ceil(dataEntries.length / elementsOnPage)
const [isBusy, setIsBusy] = useState(false)
useEffect(() => {
setIsBusy(true)
let timer = setTimeout(() => {
setIsBusy(false)
clearTimeout(timer)
}, 333)
return () => {
clearTimeout(timer)
}
}, [actualPageIdx])
const entriesOnSelectedPage = () => {
const firstEntry = (actualPageIdx - 1) * elementsOnPage
const lastEntry = firstEntry + elementsOnPage
return dataEntries.slice(firstEntry, lastEntry)
}
const entries = entriesOnSelectedPage()
const goToFirstPage = () => {
setActualPageIdx(1)
}
const goToLastPage = () => {
setActualPageIdx(lastPageIdx)
}
const goToPage = (page: number) => {
setActualPageIdx(page)
}
const goToPrevPage = () => {
setActualPageIdx((actualPageIdx) => (actualPageIdx === 1 ? actualPageIdx : actualPageIdx - 1))
}
const goToNextPage = () => {
setActualPageIdx((actualPageIdx) =>
actualPageIdx === lastPageIdx ? actualPageIdx : actualPageIdx + 1
)
}
return [
{
actualPageIdx,
lastPageIdx,
entries,
isBusy
},
{
goToFirstPage,
goToPrevPage,
goToPage,
goToNextPage,
goToLastPage,
},
]
}
Pagination
interface PaginationProps {
state: PaginationState<User>
actions: PaginationActions
}
const Pagination: FC<PaginationProps> = ({ state, actions }) => {
const pageNumbers = []
for (let i = 1; i <= state.lastPageIdx; i++) {
pageNumbers.push(i)
}
const renderPagination = pageNumbers.map((number) => (
<button
key={number}
onClick={() => actions.goToPage(number)}
className={
state.actualPageIdx === number ? styles.actualIdxStyle : styles.paginationStyle
}
>
{number}
</button>
))
return (
<div className={styles.paginationContainer}>
<button onClick={actions.goToFirstPage}>GO TO FIRST</button>
<button onClick={actions.goToPrevPage} data-testid="goToPrevPage">
<i className="fas fa-chevron-left"></i>
</button>
<div data-testid="goToPageButtons">{renderPagination}</div>
<button onClick={actions.goToNextPage} data-testid="goToNextPage">
<i className="fas fa-chevron-right"></i>
</button>
<button onClick={actions.goToLastPage}>GO TO LAST</button>
</div>
)
}
PaginatedTable
interface PaginatedTableProps {
dataEntries: User[]
}
const PaginatedTable: FC<PaginatedTableProps> = ({ dataEntries }) => {
const tableEntry = dataEntries && dataEntries.map(({ id, firstName, lastName }) => (
<div key={id} className={styles.tableStyle}>
<p>
{id}{'. '}
</p>
<p>
{firstName} {lastName}
</p>
</div>
))
return (
<div>
<div className={`${styles.tableStyle} ${styles.headStyle}`}>
<p>No.</p>
<p>Name</p>
</div>
<div data-testid="users">{tableEntry}</div>
</div>
)
}
Can confirm what #radicand mentioned in the comments above.
The following packages needed to be downgraded to these versions:
"#typescript-eslint/eslint-plugin": "3.9.1",
"#typescript-eslint/parser": "3.9.1",
"typescript": "3.9.2"
Typescript 4.x and the eslint 4.x packages don't work with react-scripts at 3.4.3.

Resources