Aly
Aly

Reputation: 361

How to access array from another file Next Js

I'm creating a Quiz App. There are two pages takeQuiz.js and result.js. I want to collect mcqs data from takeQuiz.js in result.js.I using router.replace() so user can't go back one they submit quiz.
takeQuiz.js

mcqsData = [
{quesId:1,correct:'Option A',selectedOption:'Option C'},
{quesId:1,correct:'Option C',selectedOption:'Option B'},
{quesId:1,correct:'Option D',selectedOption:'Option D'},
]

How can I access this array in result.js?. I tried import data from './takeQuiz.js' but it return undefined

Upvotes: 0

Views: 1238

Answers (1)

Colegit
Colegit

Reputation: 106

You are missing the export statement in front of your array. Without it, the import statement has nothing to grab.

export mcqsData = [
{quesId:1,correct:'Option A',selectedOption:'Option C'},
{quesId:1,correct:'Option C',selectedOption:'Option B'},
{quesId:1,correct:'Option D',selectedOption:'Option D'},
]

then in your main result.js file, import the name of your array. import {mcqsData} from './takeQuiz.js'

More info can be found here: https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

Upvotes: 2

Related Questions