I need help.
TypeError: Cannot read properties of undefined (reading 'current').
This is code โฌ๏ธโฌ๏ธ
function QuizLogic(showScore, totalTime, fetchQuestions, loading, currentQuestion, questions, handleAnswerOptionClick, countDownBarWith) {
const router = useRouter();
if (loading.current && showScore) {
return (
<button
onClick={() => router.push('/SaveComponent')}
type="button"
>
See score
</button>
);
Top comments (3)
Add a console log like that:
I would say (without having take a look at the rest of the code) that loading is something set in runtime in a different place (maybe a state) so instead adding the if before, you can do
This way it will check that
loading
is not undefined neither null before checkingloading.current
.You may also add default values for props:
I've added it just in loading but I recommend to do that on each of them. You can either use this approach, or using prop-types or both combined.
Last but not least, you may want to split your logic into different functions to keep it SOLID and KISS. ๐
Prop
loading
that you're passing to your component isundefined
somehow, I believe it is aref
for something, please check if you really initialized it correctly. It might beundefined
on initial load somehow.Other thing - you could add the Optional Chaining operator there to prevent this error:
loading is undefined. Also, not sure if this is the best place to post questions.