I’m learning the useEffect; I’m running into an exercise with a non-empty independency array since I’m passing the index of the array.
The problem is that I get same name repeated.
This is my code and below is the result.
let users = ['Oliver', 'Thomas', 'George', 'George', 'William']
export default function App() {
const [index, setIndex] = useState(0);
console.log('RENDER');
useEffect(() => {
console.log('Hello ' + users[index]);
console.log('Side Effect RUNS!');
setTimeout(() => setIndex(index => index + 1), 1000)
if (index === users.length - 1) {
return
}
}, [index])
}
RESULT:
RENDER
Hello Oliver
Side Effect RUNS!
RENDER
Hello Thomas
Side Effect RUNS!
RENDER
Hello George
Side Effect RUNS!
RENDER
Hello George
Side Effect RUNS!
RENDER
Hello William
Side Effect RUNS!
RENDER
Hello undefined
Side Effect RUNS!
RENDER
Hello undefined
Side Effect RUNS!
RENDER
Hello undefined
Side Effect RUNS!
Also as you can see, I get undefined.
How can I solve the problem?
3
Answers
It will run and print each user value and will not print undefined and will not render same output:
Try the following solution, which will make it possible for you to reiterate the list of users without displaying undefined:
Callback inside useEffect gets called every time a dependency changes, this callback will change index every time it runs and changing index will re-render component, which will cause the callback inside useEffect to re-run again and ofcourse it’s a infinite loop.
Returning from the function when index is last, won’t change anything, that’s gonna break the current call but it won’t stop from calling callback in the next render which is bound to happen.
You can add a condition when setting a timeout, to only change the value of index when there’s item next to it.