How to add – Felix Kling Jul 07 '15 at 18:53

  • (I was wrong btw, the error is correct) – Felix Kling Jul 07 '15 at 19:06
  • You may want to check this answer. [https://stackoverflow.com/questions/48966464/add-raw-html-with-script-inside-gatsby-react-page](https://stackoverflow.com/questions/48966464/add-raw-html-with-script-inside-gatsby-react-page) It works for me. This should be a comment but I don't have enough reputation... – nliu71 Mar 01 '19 at 20:28
  • 4 Answers4

    11

    I know this is old, but i stumbled upon this recently and here is the best solution i found (i'm using it for browser polyfills, but it works for any code):

    render: function() {
      return (
         <div>
            <SomeReactClass somefunction="myFunction">
            <script
              dangerouslySetInnerHTML={{ __html:
                `function myFunction(index, row) {return index;}`
              }}
            />
         </div>
      );
    }
    
    Bamieh
    • 10,358
    • 4
    • 31
    • 52
    10

    Inside JSX, {...} denotes a JavaScript expression. return index; on its own is not a valid expression.

    You have to explicitly create a string so that the {...} are not interpreted by JSX. Template literals may be the easiest solution:

    <script>{`
        function myFunction(index, row) {
            return index;
        }
    `}</script>
    

    However, I have a hard time coming up with a reason why one would want to create a <script> dynamically.


    What you should probably do is pass the function directly to the component:

    function myFunction(index, row) {
        return index;
    }
    
    var Component = React.createElement({
      render: function() {
        return (
          <div>
            <SomeReactClass somefunction={myFunction} />
          </div>
        );
      }
    });
    

    But it's hard to tell without you explaining what you are really trying to achieve here.

    Felix Kling
    • 795,719
    • 175
    • 1,089
    • 1,143
    • 1
      Fix `SomeReactClass` to expect a function, not a string. – Felix Kling Jul 07 '15 at 19:43
    • It looks like the underlying plugin expects a string attribute that corresponds to a function on the global scope https://github.com/wenzhixin/bootstrap-table/blob/master/src/bootstrap-table.js#L136. So if the OP wants to use an html attribute to initialize the bootstrap component he'll need to attach it attach it to window. Otherwise he can initialize the table manually. – Nick Tomlin Jul 07 '15 at 21:54
    1

    i think youre just looking for interpolation of your JS

    function myFunction(index, row) {
      return index;    
    }
    render: function() {
      return (
        <div>
          <SomeReactClass somefunction={myFunction}>
        </div>
      );
    }
    

    to interpolate javascript in jsx utilize curly braces {}
    https://facebook.github.io/react/docs/jsx-in-depth.html#javascript-expressions

    per your edit:
    again, you need to be using the braces to interpolate your function. so inside of SomeReactClass you need to be doing something like this in the render function:

    <div>{this.props.myFunction(index, row)}</div>
    

    most notably, you need to not only interpolate the function, by you also need to be executing it.

    PhilVarg
    • 4,762
    • 2
    • 19
    • 37
    • 1
      That doesn't make a lot of sense. This would try to render a function as a child of a `
      `.
      – Felix Kling Jul 07 '15 at 19:06
    • updated. it would be helpful for us to see the code of your example `SomeReactClass` because it doesnt look like your executing it correctly. Id also suggest reading the docs to how to properly use jsx from the link i posted – PhilVarg Jul 07 '15 at 19:45
    0

    You should try this:

    function myFunction(index, row) {
      return index;    
    }
    
    render: function() {
     return (
          <div>
            <script
                dangerouslySetInnerHTML={{ 
                  __html:myFunction() 
            
                }}
            />
          </div>
          );
    }
    
    Visnja
    • 1