简体   繁体   English

如何使用 <Link /> 在dangerouslySetInnerHTML中的组件

[英]How to use <Link /> component inside dangerouslySetInnerHTML

Currently I have this in one of my components: 目前我在我的一个组件中有这个:

{someObject.map(obj => (
    <div
        dangerouslySetInnerHTML={{
            __html: obj.text
        }}
    />
))}

Basically, I am mapping over someObject which on another file. 基本上,我映射到另一个文件上的someObject The structure is like this: 结构是这样的:

export default someObject = [
      {
         obj: "<p>Some text 1.</p>"
      },
      {
         obj: "<p>Some text 2.</p>"
      }
    ]

I'm just simplifying the content for demonstration's sake. 我只是为了演示而简化内容。 However, I ran into a problem because I need to use the <Link /> component in one of the items. 但是,我遇到了一个问题,因为我需要在其中一个项目中使用<Link />组件。 As in: 如:

export default someObject = [
    {
        obj: "<p>Some text 1.</p>"
    },
    {
        obj: "<p>Some text 2.</p>"
    },
    {
        obj: "<p>Some text 2 and <Link to="/someroute">link</Link>.</p>"
    }
]

However, it's not working because that entire <p></p> tag is wrapped in dangerouslySetInnerHTML . 但是,它不起作用,因为整个<p></p>标记都包含在dangerouslySetInnerHTML

I can just use plain <a></a> tag for the link but that doesn't seem like a good solution as the entire application would reload instead of just going to another route. 我可以使用简单的<a></a>标记作为链接,但这似乎不是一个好的解决方案,因为整个应用程序将重新加载而不是仅仅转到另一个路由。

What are the other options to make this work? 使这项工作的其他选择是什么?

Why don't you just export the object as a jsx object? 为什么不直接将对象导出为jsx对象? I think use dangerouslySetInnerHTML is a bad practice, it might cause XSS attack. 我认为使用dangerouslySetInnerHTML是一种不好的做法,它可能会导致XSS攻击。

 const someObject = [ { obj: <p>Some text 1.</p> }, { obj: <p>Some text 2.<a href="https://google.com">google</a></p> } ] class App extends React.Component { render(){ return ( <div className="App"> <h1>Hello world</h1> <h2>Jsx object goes here {someObject[1].obj}</h2> </div> )}; } const rootElement = document.getElementById("container"); ReactDOM.render(<App />, rootElement); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.2/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.6.2/react-dom.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

  • There's is two ways to solve this problem : 有两种方法可以解决这个问题:

First Way : 第一种方式:

it's like a more general approach you can use it to opt your code. 它就像一种更通用的方法,您可以使用它来选择代码。 try to use this library ( https://github.com/tasti/react-linkify/ ) 尝试使用此库( https://github.com/tasti/react-linkify/

Here's the simpler form of the component : 这是组件的简单形式:

 import React, {PropTypes} from 'react'; import Linkify from 'react-linkify'; export default class TextWithLink extends React.Component { constructor(props) { super(props); } render() { let text = this.props.text; if(this.props.showLink) { text = <Linkify properties={{target: '_blank', rel: "nofollow noopener"}}>{text}</Linkify> } return (<div>{text}</div>); } } 

Second Way : 第二种方式:

In case, if you want to create a hyperlink ( <a> ), you should have a function which builds elements and returns the result. 如果你想创建一个超链接( <a> ),你应该有一个构建元素并返回结果的函数。

Example : 示例:

 list = { text: 'hello world', link: 'www.facebook.com' } 

And the render function could be something like : 渲染函数可能是这样的:

 buildLink() { return( <p> {list.text}. <a href={list.link}>{list.link}</a> </p> ); } render() { return (this.buildLink()); } 

export default someObject = [
    {
        obj: "<p>Some text 1.</p>"
    },
    {
        obj: "<p>Some text 2.</p>"
    },
    {
        obj: linkto('/someroute')
    }
]

linkto will solve your issue. linkto将解决您的问题。

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

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