Search code examples
reactjsreact-dom

react.js : javascript in angle brackets not working


For some reason when I try and enter JavaScript into the angle part of the HTML portion it does not work. I'm using Thymeleaf.

Here's the HTML portion:

   <button className="btn btn-info" onClick={this.displayInfo}>Click for info about {firstName[i]}</button>

So in this instance it does not work and returns an error:

unexpected error (type=Internal Server Error, status=500) Exception parsing document

If I remove the onClick={this.displayInfo} it will work.

my function:

   displayInfo(){
        console.log("it worked");
    }

The render() function:
    render(){
        var {number, firstName, lastName} = this.state;
        var rows = [];
        for (var i in number)
        {
           rows[i] = (<tr>
                        <td>{number[i]}</td>
                        <td>{firstName[i]}</td>
                        <td>{lastName[i]}</td>
                        <td><button className="btn btn-info" onClick={this.displayInfo()}>Click for info about {firstName[i]}</button></td>
                      </tr>);
        }
        var headers = <tr><th>Number</th><th>First Name</th><th>Last Name</th><th>Extra Info</th></tr>;
        return (<table className="table table-bordered">
                                    <thead>{headers}</thead>
                                    <tbody>{rows}</tbody>
                                    </table>);
    }

Solution

  • I was getting the error because Thyemleaf thought that I was typing HTML: I just added this:

      /*<![CDATA[*/
      ...
      /*]]>*/