reactjs - 无法读取未定义的属性“地图”。打字稿和反应
问题描述
我刚开始练习 TypeScript 和 React,我不知道我在这个问题上哪里出错了。他们只是一直告诉我'无法读取未定义的属性'地图'。有人能告诉我哪里错了吗?
// Main files for application
import React,{useState} from 'react';
import {fetchQuizQuestions} from './API';
// Components
import QuestionCard from './components/QuestionCard';
// Types
import {QuestionState, Difficulty} from './API';
type AnswerObject = {
question :string;
answer :string;
correct :boolean;
correctAnswer :string;
}
const TOTAL_QUESTIONS = 10;
const App =() =>{
const[loading,setLoading] = useState(false);
const[questions,setQuestions] = useState<QuestionState[]>([]);
const[number,setNumber] = useState(0);
const[userAnswer,setUserAnswer] = useState<AnswerObject[]>([]);
const[score,setScore] = useState(0);
const[gameOver,setGameover] = useState(true);
console.log(questions);
const startTrivia = async () =>{
setLoading(true);
setGameover(false);
const newQuestions = await fetchQuizQuestions(
TOTAL_QUESTIONS,
Difficulty.EASY
);
setQuestions(newQuestions);
setScore(0);
setUserAnswer([]);
setNumber(0);
setLoading(false);
};
const checkAnswer = (e: React.MouseEvent<HTMLButtonElement>)=>{
}
const nextQuestion=()=>{
}
return (<div className="App">Quiz
<h1>REACT QUIZ</h1>
{
gameOver || userAnswer.length === TOTAL_QUESTIONS?(
<button className="start" onClick={startTrivia}>
Start
</button>
):null}
{!gameOver ? <p className="score">Score</p>: null}
{loading ? <p>Loading Questions...</p> : null}
{!loading && !gameOver &&(
<QuestionCard
questionNumber ={number + 1}
totalQuestions = {TOTAL_QUESTIONS}
question={questions[number].question}
answers ={questions[number].answers}
userAnswer = {userAnswer ? userAnswer[number]: undefined}
callback={checkAnswer}
/>
)}
{!gameOver &&
!loading &&
userAnswer.length === number + 1 &&
number !== TOTAL_QUESTIONS - 1 ? (
<button className="next" onClick={nextQuestion}>
Next Question
</button>
):null }
</div>
);
}
export default App;
这是我一直在处理的另一个 QuestionCard 文件。控制台显示问题出在“answers.map”上。
import React from 'react';
type Props ={
question:string;
answers:string[];
callback:any;
userAnswer:any;
questionNumber : number;
totalQuestions:number;
};
const QuestionCard:React.FC<Props>=({
question,
answers,
callback,
userAnswer,
questionNumber,
totalQuestions,
})=>(
<div>
<p className='number'>
Question:{questionNumber} / {totalQuestions}
</p>
<p dangerouslySetInnerHTML={{ __html:question}}/>
<div>
{answers.map((answer)=>(
<div key={answer}>
<button disabled={userAnswer} onClick={callback}>
<span dangerouslySetInnerHTML={{ __html:answer}}/>
</button>
</div>
))}
</div>
</div>
);
export default QuestionCard;
解决方案
QuestionCard.jsx
<div>
{/* make sure the `answers` exist */}
{answers && answers.map((answer)=>(
<div key={answer}>
<button disabled={userAnswer} onClick={callback}>
<span dangerouslySetInnerHTML={{ __html:answer}}/>
</button>
</div>
))}
</div>
推荐阅读
- c# - 更新目录中的 XML 文件
- bixby - AudioItem.albumArt 应该有什么分辨率?
- ruby-on-rails - rails 是否应该使用来自不同路径的 gem?
- asp.net - 具有多个路由的 Web API 导致错误
- sql - 如何使用 Visual Studio 连接到 SQL Server 数据库(Windows 身份验证)
- plsql - 在表格中插入一个没有特殊字符的数字
- nginx - 我是否需要一项服务来公开在 pod 中运行的每个应用程序?
- autodesk-forge - Autodesk Forge - 使用 modelderivative API 从 2D Cad 绘图中提取几何数据
- json - 如何将数据从电报机器人内联键盘发送到谷歌电子表格中的特定列?
- python - 用于 Dialogflow 的 Python 中的 Fulfillment Webhook