Search code examples
javascriptreactjsreact-rails

JSX can't parse partial HTML tag


I'm using react-rails and I need to be able to pass just a start HTML tag (ie: <form>) and just an end tag into an array (ie: </form>) in my JSX file. When I try doing either or I get a ExecJS::RuntimeError that says SyntaxError: unknown: Unexpected token.

This doesn't work:

array.push(<form>)

This works:

array.push(<form></form>)


Solution

  • JSX is syntactic sugar for creating tags in JavaScript using the React library.

    <form></form>
    

    in JSX compiles to:

    React.createElement("form", null);
    

    (Try it!)

    JSX is not "HTML as a string" or whatever other way you might be thinking it works. It simply does not work the way you seem to want it to.

    Without more detail about what exactly you want to achieve with this, we cannot help you further.