0

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

6
  • http://localhost:8082/like_button.js is this js available from that URL? Put it in the public folder of spring boot app, first it should be available at that location Commented Apr 23, 2022 at 12:43
  • can you show your like_button.js source? Looks like you're trying to use JSX (which requires further configurations)? Commented Apr 23, 2022 at 13:37
  • The like_button.js is here and then from the tutorial it adds the const domContainer = document.querySelector('#like_button_container'); ReactDOM.render(e(LikeButton), domContainer); Commented Apr 23, 2022 at 13:57
  • 1
    There is no < character in there, no JSX indeed, so why is console showing error about < character Commented Apr 23, 2022 at 13:59
  • It comes from step 3 here Commented Apr 23, 2022 at 14:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.