admin管理员组文章数量:1334313
I'm rendering some paragraphs using React. Inside those paragraphs there should be a span for footnotes. The best I got so far is that it rendered [object Object].
function ArticleItem() {
const articles = [
{
title: "title",
content: [
`Text based on this footnote <span>1</span>`,
`Another argument, here is my source <span>2</span>`
]
}
];
return (
<div className="article-container">
<h3> {articles[i].title} </h3>
{
articles[i].content.map(paragraph => (
<p>
{ paragraph }
</p>
)
}
</div>
);
}
I'm rendering some paragraphs using React. Inside those paragraphs there should be a span for footnotes. The best I got so far is that it rendered [object Object].
function ArticleItem() {
const articles = [
{
title: "title",
content: [
`Text based on this footnote <span>1</span>`,
`Another argument, here is my source <span>2</span>`
]
}
];
return (
<div className="article-container">
<h3> {articles[i].title} </h3>
{
articles[i].content.map(paragraph => (
<p>
{ paragraph }
</p>
)
}
</div>
);
}
Share
Improve this question
asked Jan 4, 2020 at 19:31
Moshe MMoshe M
911 gold badge1 silver badge8 bronze badges
3
- Maybe this answer your question – Erick ls Commented Jan 4, 2020 at 19:35
- Hi Moshe. I saw that you have a very low ratio of questions/accepted answers, but you are menting on the answers "thank you, it works" and things like that. The proper way to accept an answer is by clicking the green check on the left of the answer. This is mandatory to have StackOverflow clean – Jorge Fuentes González Commented Mar 20, 2020 at 17:37
- Ok, I will use it next time. – Moshe M Commented Mar 22, 2020 at 13:18
2 Answers
Reset to default 2Because you are creating a string with "<span>"
instead of creating actual <span>
HTML elements. What you are using is named jsx
, which converts HTML tags to their corresponding document.createElement()
(or similar, which in React has it own way).
If you want the content
to be an HTML element and not a string, then create an HTML element:
function ArticleItem() {
const articles = [
{
title: "title",
content: [
<p>Text based on this footnote <span>1</span></p>,
<p>Another argument, here is my source <span>2</span></p>
]
}
];
return (
<div className="article-container">
<h3> {articles[i].title} </h3>
{ articles[i].content.map(paragraph => paragraph) }
</div>
);
}
Notice how I removed the string literal (``) and created an HTML element.
If the article content es from an API call, then avoid using HTML tags inside strings. That's a bad practice actually. Always create the tags within the render()
call and populate them with the API data you received.
Assuming the content
array has to be supplied as raw HTML, a mon solution is to use the dangerouslySetInnerHTML
prop to render that HTML directly. You can introduce that to your <p>
elements like this:
articles[i].content.map(paragraph => (
<p dangerouslySetInnerHTML={{ __html: paragraph }} />
))
A few other things to consider; I noticed a missing )
in your code after the <p>
element of your map
statement which will be causing a syntax error. Also, ensure that i
is defined to an index in range of your articles
array. Here's an example of each fix applied:
function ArticleItem() {
const articles = [
{
title: "title",
content: [
`Text based on this footnote <span>1</span>`,
`Another argument, here is my source <span>2</span>`
]
}
];
const i = 0;
return (
<div className="article-container">
<h3> {articles[i].title} </h3>
{
articles[i].content.map(paragraph =>
(<p dangerouslySetInnerHTML={{ __html: paragraph }} />)
)
}
</div>
);
}
Here's a working example as well - hope that helps!
本文标签: javascriptHow Can I Insert ltspangt To a Rendered Text In ReactStack Overflow
版权声明:本文标题:javascript - How Can I Insert <span> To a Rendered Text In React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742346802a2457684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论