简体   繁体   English

在React组件中使用的字符串变量内插入html

[英]Inserting html inside string variable used in React component

I have a react app I am building for my portfolio site. 我有一个正在为我的投资组合网站构建的React应用程序。 Currently i have the app written in JSX so i can add stuff like: 目前,我已经用JSX编写了应用程序,因此我可以添加以下内容:

class Project extends React.Component {
render() {
    return (
        <div className={this.props.project.class}>
            <h1>{this.props.project.header}</h1>
            <div>
                <div className='description'>
                    <p>{this.props.project.description}</p>
                    <p>The Tech Stack</p>
                    <ul>
                        {
                        this.props.project.items.map(function(listValue){
                            return <li>{listValue}</li>;
                        })}
                    </ul>
                </div>
                <div className='image'>

                </div>
            </div>

        </div>
    );
}

} }

to my component. 到我的组件。 I am currently managing my long strings of text in a different file called Text.js: 目前,我正在另一个名为Text.js的文件中管理我的长字符串文本:

export let exampleText = "Graded is a application developed by <a href='google.com'> Link here. </a> and myself for our second year user interfaces course.";

And access it in my jsx file here: 并在我的jsx文件中访问它:

var gradedItems = {
    class: "gradedSection content",
    header: "Graded",
    description: Text.exampleText,
    items : ["Java", "Android"]
}

and then it gets displayed in the <p> tags in the above component. 然后将其显示在上述组件的<p>标记中。 However as I mentioned the link doesn't actually render and the html tags don't get rendered. 但是,正如我提到的那样,链接实际上不会呈现,并且html标签不会呈现。 So it looks like some text and then a tag in the website. 因此,它看起来像是一些文本,然后是网站中的标签。

How should I go about adding these html tags within this string so when the component renders it, the html elements inside get rendered out? 我应该如何在这些字符串中添加这些html标签,以便在组件呈现它时,呈现其中的html元素?

Is it even possible in this situation, is there a better way of doing what I'm trying to achieve? 在这种情况下是否有可能,是否有更好的方法来完成我要达到的目标?

Thanks as always SO! 一如既往的感谢!

You just need to use dangerouslySetInnerHTML and it will help. 您只需要使用危险地使用SetInnerHTML ,它将有所帮助。

Example usage: 用法示例:

let exampleText = "Graded is a application developed by <a href='google.com'> Link here. </a> and myself for our second year user interfaces course.";
function Component() {
  return <div dangerouslySetInnerHTML={{__html: exampleText }} />;
}

An option is to change the strings in Text.js to html bits, like 一个选项是将Text.js的字符串Text.js为html位,例如

export const exampleText = (
  <span>
    Graded is a application developed by 
    <a href='google.com'> Link here. </a>
    and myself for our second year user interfaces course.
  </span>
);

first you npm install html-react-parser 首先您npm安装html-react-parser

import Parser from 'html-react-parser';

var exampleText = 'Graded is a application developed by <a href='google.com'>     Link here. </a> and myself for our second year user interfaces course.';


render: function() {
    return (
        <div>{Parser(exampleText)}</div>
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM