重新加载导入的变量 React Native

Reload Imported variable React Native

我目前有这个文件English.js

const questions = [
  "what is your name?",
  "How old are you?",
  "Where are you from?",
  "You speak English?",
  "How long have you been speaking English?",
  "What is your favorite color?",
  "What are your hobbies?",
  "What is your first language?",
  "What is your occupation?",
  "Who is your role model?"
]

const englishQuiz = []
const indexing = []
while (indexing.length < 5) {
  let rand = Math.floor(Math.random() * questions.length)
  if (!indexing.includes(rand)) {
    indexing.push(rand)
    englishQuiz.push(questions[rand])
  }

}
console.log(englishQuiz)
export default englishQuiz
在另一个文件 Quiz.js 中,我导入了 englishQuiz

import  englishQuiz from '../data/English'

完成测验后,我弹出堆栈以转到上一页。但是当我去 再次回到问答页面,还是同样的问题。每次我通过 English.js 中的随机循环进入测验页面时,我都试图重新整理这些问题。请帮忙

而不是直接导出一个函数来打乱你的问题,然后 return 然后在需要的时候使用它。

const questions = [
  ...
]
 
export const getEnglishQuiz = () => {
  const englishQuiz = []
  const indexing = []
  while (indexing.length < 5) {
    let rand = Math.floor(Math.random() * questions.length)
    if (!indexing.includes(rand)) {
      indexing.push(rand)
      englishQuiz.push(questions[rand])
    }
  }
  return englishQuiz
}

现在在您的组件中,您可以在屏幕聚焦时使用此功能。 因此,每当您的屏幕再次聚焦时,您应该调用此函数以再次获取新问题。