[英]dangerouslySetInnerHTML showing object on mapped data
I tried getting some values from an GraphQL data.我尝试从 GraphQL 数据中获取一些值。 Then, I want to display it via然后,我想通过<div dangerouslySetInnerHTML={{ __html: ExtendedProfileShow }} />
Then, I got [object Object],[object Object],[object Object]
.然后,我得到[object Object],[object Object],[object Object]
。
Here are my codes:这是我的代码:
The error File:错误文件:
export default function Profile({ profileData }) {
const ExtendedProfileShow = [
profileData.businessInfo.extendedProfile.map((showExtendedProfile) => (
<div key={showExtendedProfile.title}>
{showExtendedProfile.title} <br/> {showExtendedProfile.info}
</div>
))
]
return (
<>
<div dangerouslySetInnerHTML={{ __html: ExtendedProfileShow }} />
</>
);
}
My api.js:我的 api.js:
export async function getAllBusinessProfiles() {
const data = await fetchAPI(
`
query AllProfiles {
businessProfiles(where: {orderby: {field: DATE, order: ASC}}) {
edges {
node {
date
title
slug
link
uri
businessInfo {
name
title
company
image {
mediaItemUrl
altText
}
highlight
phone
city
country
facebook
linkedin
instagram
email
website
profiles {
profile
profileInfo
}
extendedProfile {
title
info
}
}
}
}
}
}
`
);
return data?.businessProfiles;
};
The data has HTML elements like - <p> Test test </p>
And it's expected to execute the tags also.数据具有 HTML 元素,例如 - <p> Test test </p>
并且预计也会执行标签。
What could be error here?这里可能是什么错误? Thanks.谢谢。
In this case there's no need to even use dangerouslySetInnerHTML
.在这种情况下,甚至不需要使用dangerouslySetInnerHTML
。 You can simply pass the JSX you generate directly.您可以直接传递您生成的 JSX。
export default function Profile({ profileData }) {
const ExtendedProfileShow = [
profileData.businessInfo.extendedProfile.map((showExtendedProfile) => (
<div key={showExtendedProfile.title}>
{showExtendedProfile.title} <br/> {showExtendedProfile.info}
</div>
))
]
return (
<>
<div>{ExtendedProfileShow}</div>
</>
);
}
However, if for some reason you really wanted to use dangerouslySetInnerHTML
, you could use ReactDOMServer.renderToStaticMarkup
to convert your JSX to a string.但是,如果出于某种原因您真的想使用dangerouslySetInnerHTML
,您可以使用ReactDOMServer.renderToStaticMarkup
将您的 JSX 转换为字符串。
import ReactDOMServer from 'react-dom/server'
export default function Profile({ profileData }) {
const ExtendedProfileShow = [
profileData.businessInfo.extendedProfile.map((showExtendedProfile) => (
<div key={showExtendedProfile.title}>
{showExtendedProfile.title} <br/> {showExtendedProfile.info}
</div>
))
]
return (
<>
<div dangerouslySetInnerHTML={{
__html: ReactDOMServer.renderToStaticMarkup(ExtendedProfileShow)
}} />
</>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.