React: Script tag not working when inserted using dangerouslySetInnerHTML

后端 未结 5 1367
甜味超标
甜味超标 2020-11-28 12:29

I\'m trying to set html sent from my server to show inside a div using dangerouslySetInnerHTML property in React. I also have script tag inside it and use functions defined

5条回答
  •  生来不讨喜
    2020-11-28 13:16

    Here's a bit of a dirty way of getting it done , A bit of an explanation as to whats happening here , you extract the script contents via a regex , and only render html using react , then after the component is mounted the content in script tag is run on a global scope.

    var x = 'alert("this.is.sparta");function pClicked() {console.log("p is clicked");}

    Hello

    '; var extractscript=/
提交回复
热议问题