类型“() => Element”不可分配给类型 'string'
Type '() => Element' is not assignable to type 'string'
问候!
我有一个传统的 function
,我返回一个 span
和一个 prop
(如果我没记错的话)。
在我的 ts 代码上我有这个错误
Error image
这是我的 code.The 文件名是 qCard.tsx
import { QuestionAnswerTwoTone } from "@material-ui/icons";
import React from "react";
// crear props
type Props = {
question: string;
answers: string[];
callback: any;
userAnswer: boolean;
questionNm: number;
totalQuestions: number;
};
// A function statement declared component that takes no children.
export default function QuestionCard({
question,
answers,
callback,
userAnswer,
questionNm,
totalQuestions,
}: Props) {
//duda
return (<div>
<p> Question : {questionNm} / {totalQuestions} </p>
<p dangerouslySetInnerHTML={{ __html: question }} />
<div>
{answers.map(function answer(){
return( <div>
<button disabled={userAnswer} onClick={callback}>
<span dangerouslySetInnerHTML={{ __html: answer }}/>
</button>
</div>);
} )}
</div>
</div>
);
}
(错误行)
我曾尝试删除 {{ **__html**: answer }}
,但它没有用。
这是因为,您正在为 __html 使用函数名称(React 中的一个元素):
answers.map(function answer(){
// __html: answer ^^
改用箭头函数:
answers.map(answer => {
// __html: answer // now it works.
或者,如果您希望继续使用函数语句,那么您可以传递一个参数并使用:
answers.map(function answer(ans){
// __html: ans
问候!
我有一个传统的 function
,我返回一个 span
和一个 prop
(如果我没记错的话)。
在我的 ts 代码上我有这个错误
Error image
这是我的 code.The 文件名是 qCard.tsx
import { QuestionAnswerTwoTone } from "@material-ui/icons";
import React from "react";
// crear props
type Props = {
question: string;
answers: string[];
callback: any;
userAnswer: boolean;
questionNm: number;
totalQuestions: number;
};
// A function statement declared component that takes no children.
export default function QuestionCard({
question,
answers,
callback,
userAnswer,
questionNm,
totalQuestions,
}: Props) {
//duda
return (<div>
<p> Question : {questionNm} / {totalQuestions} </p>
<p dangerouslySetInnerHTML={{ __html: question }} />
<div>
{answers.map(function answer(){
return( <div>
<button disabled={userAnswer} onClick={callback}>
<span dangerouslySetInnerHTML={{ __html: answer }}/>
</button>
</div>);
} )}
</div>
</div>
);
}
(错误行)
我曾尝试删除 {{ **__html**: answer }}
,但它没有用。
这是因为,您正在为 __html 使用函数名称(React 中的一个元素):
answers.map(function answer(){
// __html: answer ^^
改用箭头函数:
answers.map(answer => {
// __html: answer // now it works.
或者,如果您希望继续使用函数语句,那么您可以传递一个参数并使用:
answers.map(function answer(ans){
// __html: ans