not<...">

React - How to pass HTML tags in requisites?

I want to be able to pass text using HTML tags, for example:

<MyComponent text="This is <strong>not</strong> working." /> 

But inside the MyComponent rendering MyComponent , when I print this.props.text , it literally prints everything:

 This is <strong>not</strong> working. 

Is there a way to get React to parse the HTML and upload it correctly?

+46
reactjs
Oct 28 '15 at 0:45
source share
10 answers

You can use mixed arrays with strings and JSX elements (see docs here ):

 <MyComponent text={["This is ", <strong>not</strong>, "working."]} /> 

There is a fiddle here that shows that it works: http://jsfiddle.net/7s7dee6L/

In addition, as a last resort, you always have the opportunity to embed raw HTML , but be careful because this can open the cross (XSS) for you if they do not sanitize the property values.

+59
Oct 28 '15 at 1:14
source share
β€” -

There are actually several ways to go about this.

Do you want to use JSX inside your details

You can simply use {} to force JSX to parse the parameter. The only limitation is the same as for each JSX element: it should return only one root element.

 myProp={<div><SomeComponent>Some String</div>} 

The most readable way to do this is to create a renderMyProp function that will return the JSX components (like the standard rendering function) and then just call myProp = {this.renderMyProp ()}

You want to pass only HTML as a string

By default, JSX does not allow you to display raw HTML from string values. However, there is a way to do this:

 myProp="<div>This is some html</div>" 

Then in your component you can use it like this:

 <div dangerouslySetInnerHTML={this.props.myProp}></div> 

Beware that this solution may β€œopen” when attacking fake scripts. Also be careful that you can only display plain HTML, not a JSX tag or component or other interesting things.

Array method

In response, you can pass an array of JSX elements. It means:

 myProp={["This is html", <span>Some other</span>, "and again some other"]} 

He would not recommend this method because:

  • A warning will be generated (missing keys)
  • Not readable
  • This is actually not a JSX method, it is rather a hack than the intended design.

Children's way

By adding this for completeness, but in reaction, you can also get all the children that are β€œinside” your component.

So, if I take the following code:

 <SomeComponent> <div>Some content</div> <div>Some content</div> </SomeComponent> 

Then two divs will be available as this.props.childrens in SomeComponent and can be displayed using the standard {} syntax.

This solution is ideal when you have only one HTML content to pass to your component (imagine a Popin component that only accepts Popin content as children).

However, if you have multiple content, you cannot use children (or you need to at least combine them with another solution here)

+17
Sep 29 '17 at 7:59
source share

You can use dangerouslySetInnerHTML

Just send html as normal line

 <MyComponent text="This is <strong>not</strong> working." /> 

And do the visualization in JSX code as follows:

 <h2 className="header-title-right wow fadeInRight" dangerouslySetInnerHTML={{__html: props.text}} /> 

Just be careful if you provide user input. You may be a victim of an XSS attack.

Here's the documentation: https://facebook.imtqy.com/react/tips/dangerously-set-inner-html.html

+7
Oct 20 '16 at 18:31
source share
 <MyComponent text={<span>This is <strong>not</strong> working.</span>} /> 

and then in your component you can do so to check:

 import React from 'react'; export default class MyComponent extends React.Component { static get propTypes() { return { text: React.PropTypes.object, // if you always want react components text: React.PropTypes.any, // if you want both text or react components } } } 

Make sure that you select only one type of support.

+5
Nov 02 '16 at 11:07
source share

It worked for me, passing the html tag in the details of the children

 <MyComponent>This is <strong>not</strong> working.</MyComponent> var MyComponent = React.createClass({ render: function() { return ( <div>this.props.children</div> ); }, 
+4
03 Feb '17 at 21:46
source share

You can do this in two ways that I know of.

1- <MyComponent text={<p>This is <strong>not</strong> working.</p>} />

And then do it

 class MyComponent extends React.Component { render () { return (<div>{this.props.text}</div>) } } 

Or the second approach does it like this:

2- <MyComponent><p>This is <strong>not</strong> working.</p><MyComponent/>

And then do it

 class MyComponent extends React.Component { render () { return (<div>{this.props.children}</div>) } } 
+2
Sep 27 '17 at 7:53 on
source share

You can also use the function on the component to transfer via jsx through the details. as:

  var MyComponent = React.createClass({ render: function() { return ( <OtherComponent body={this.body} /> ); }, body() { return( <p>This is <strong>now</strong> working.<p> ); } }); var OtherComponent = React.createClass({ propTypes: { body: React.PropTypes.func }, render: function() { return ( <section> {this.props.body()} </section> ); }, }); 
+1
Nov 04 '16 at 14:26
source share

In my project, I had to pass a dynamic html fragment from a variable and make it inside the component. So I did the following.

 defaultSelection : { innerHtml: {__html: '<strong>some text</strong>'} } 

The defaultSelection object is passed to the component from the .js file

 <HtmlSnippet innerHtml={defaultSelection.innerHtml} /> 

HtmlSnippet Component

 var HtmlSnippet = React.createClass({ render: function() { return ( <span dangerouslySetInnerHTML={this.props.innerHtml}></span> ); } }); 

Plunkr example

edit doc for dangerous SetInnerHTML

0
Mar 03 '17 at 9:43 on
source share

Yes, you can do this using a mix array with strings and JSX elements. link

 <MyComponent text={["This is ", <strong>not</strong>, "working."]} /> 
0
01 Oct '17 at 16:00
source share

Applied html to componentDidMount using jQuery append. This should solve the problem.

  var MyComponent = React.createClass({ render: function() { return ( <div> </div> ); }, componentDidMount() { $(ReactDOM.findDOMNode(this)).append(this.props.text); } }); 
-7
May 10 '16 at 5:51
source share



All Articles