Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js - conditionally use dangerouslySetInnerHTML

I have some code to Regex some text and wrap it in a <span /> like so:

highlightQuery() {
    // will output the text response from the Model, but also highlight relevant words if they match the search query
    // that the user input
    let input = this.props.model.get('value');

    if(!this.state.hasMatch){
        return input;
    }

    let query = this.props.matched.query,
        index = this.props.model.get('searchIndexes')[this.props.matched.index];

    const replaceBetween = (str, start, end, what) => {
        return str.substring(0, start) + what + str.substring(start + end);
    };

    let ret = replaceBetween(input, index, query.length, `<span class='highlighted'>${query}</span>`);

    return ret;
},

render() {

    const classes = classNames(
        this.props.model.get('type'),
        'character'
    );

    return (
        <span key={this.props.model.cid} className={classes} dangerouslySetInnerHTML={ {__html: this.highlightQuery()} }>
            {!this.state.hasMatch && this.highlightQuery()}
        </span>
    );
}

However, this yields: Uncaught Error: Invariant Violation: Can only set one of children or props.dangerouslySetInnerHTML.

How am I best to conditionally use dangerouslySetInnerHTML?

like image 662
benhowdle89 Avatar asked Aug 05 '15 14:08

benhowdle89


People also ask

What can I use instead of dangerouslySetInnerHTML in React?

However, if the application or site accepts multiple users' data input, you should be concerned. However, apart from that, there is one alternative to using dangerouslySetInnerHTML, simply setting innerHTML of a React HTML element using vanilla JS instead.

How do you use dangerouslySetInnerHTML React?

dangerouslySetInnerHTML is a property that you can use on HTML elements in a React application to programmatically set their content. Instead of using a selector to grab the HTML element, then setting its innerHTML , you can use this property directly on the element.

Why do we use dangerouslySetInnerHTML?

The name dangerouslySetInnerHTML is intentionally chosen to remind you that it's dangerous and may cause XSS vulnerabilities so that you make sure your HTML is sanitized before you insert it in your react app.

How do you select the DOM element in React?

In React we can access the DOM element using Refs. Refs provide a way to access DOM nodes or React elements created in the render method. Creating Refs: Refs are created using React. createRef() and attached to React elements via the ref attribute.


2 Answers

and make sure you do not include a space between the span tags else you would run into the following error:

invariant.js:39 Uncaught Invariant Violation: Can only set one of children or props.dangerouslySetInnerHTML.

return = <span key={this.props.model.cid} className={classes} dangerouslySetInnerHTML={ {__html: this.highlightQuery()} }>NO SPACE OR TEXT HERE</span>
like image 128
Manoj Amalraj Avatar answered Nov 03 '22 01:11

Manoj Amalraj


You can use spread syntax if you are using ES2016.

const options = {};
if (useHTML) {
  options.dangerouslySetInnerHTML = {__html: yourHTML};
} else {
  options.children = [<ChildNode/>];
}
return <span {...options} className="myClassName"></span>
like image 20
xcodebuild Avatar answered Nov 03 '22 00:11

xcodebuild