Adding script tag to React/JSX

后端 未结 17 1114
感动是毒
感动是毒 2020-11-22 03:52

I have a relatively straightforward issue of trying to add inline scripting to a React component. What I have so far:

\'use strict\';

import \'../../styles/         


        
17条回答
  •  不要未来只要你来
    2020-11-22 04:16

    To add script tag or code in head tag , use react-helmet package. it is light and have good documentation.

    To add Js code in script tag inside body,

        function htmlDecode(html) {
          return html.replace(/&([a-z]+);/ig, (match, entity) => {
            const entities = { amp: '&', apos: '\'', gt: '>', lt: '<', nbsp: '\xa0', quot: '"' };
            entity = entity.toLowerCase();
            if (entities.hasOwnProperty(entity)) {
              return entities[entity];
            }
            return match;
          });
        }
      render() {
        const scriptCode = ``
        return(
          
    ; ); }

    this code can be tested by console.log(windows.hello)

提交回复
热议问题