'Add React in One Minute script to Spring Boot controller

At Add React to a Website there's simple instructions to add a React component into a web page. This works fine run directly as a single page.

react in one minute works

However, if the same page is "hosted" in a spring boot app, with a single route per this answer

@Controller
public class HomeController {
  @GetMapping("/**")
  public String react() {
      return "index.html";
  }
}

the text renders but not the react component.

no like component

The error in the console is Uncaught SyntaxError: Unexpected token '<'

uncaught syntaxError

If I try a direct route mapping

@GetMapping("/test")
public String react() {
    return "index.html";
}

Again the react component doesn't display

again no like component

It's a GET http://localhost:8082/like_button.js net::ERR_ABORTED 404 error

404 error

Is the problem that to run a react script in spring boot you have to install separate React app dependencies?

/* UPDATE */

It's something to do with the JSX and Babel per this answer. If I add to the index.html

<script src="https://unpkg.com/@babel/standalone/babel.min.js"> </script>

<!-- Load our React component. -->
<script type="text/babel" src="like_button.js"></script>

there's no longer an Uncaught SyntaxError: Unexpected token '<' error. The error is now:

Uncaught SyntaxError: /http:/localhost:8082/like_button.js: Unexpected token (11:5)

unexpected token



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source