3

I am creating Spring boot application and i want my frontend to be with React. The problem comes from the fact that i cannot find a way to properly integrate the react component in my jsp page.

Here is the declaration of the component:

ReactDOM.render(<App />, document.getElementById('root'));

I want to point out that my jsp page is inside WEB-INF directory and i can successfully redirect to it via a controller, but my import of the component does not work and does not throw an error!

I tried to import it by the element id:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Intellij is trash</title>
</head>
<body>
ala bala

<div id="root"></div>

<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
</body>
</html>

Am i doing something wrong or missed some configuration?

Thanks in advance for your help!

3
  • Anyway, your JSP file does not import the JavaScript with the React app. Commented Nov 21, 2019 at 14:06
  • @JozefChocholacek i thought if there is a visual representation it would be more helpful. I am new to react how should i import the JavaScript with the React? Commented Nov 21, 2019 at 14:10
  • Ehm, ehm, what about reading the tutorial? Just instead the like_button.js use the file name where your ReactDOM.render(<App />, document.getElementById('root')); is. Commented Nov 21, 2019 at 14:44

1 Answer 1

4

After importing the React and ReactDOM scripts, import another script where you have written your component App. Assuming it is in App.js,

<script src="App.js"></script>

Now you need to render the component in the div with id="root". I will use babel to compile but you can do without it too. So after the import write:

<script type="text/babel">
ReactDOM.render(<App />, document.getElementById('root'));
</script>

Conclusion: Basically you need to import 4 files in this order: React, ReactDOM, Babel and your component.js files. Then you need to render the component in the div for it to work.

<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="App.js"></script>

<script type="text/babel">
    ReactDOM.render(<App />, document.getElementById('root'));
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

i tried this way and the react dev tools seem to detect that there is react in the page, but it does not display it. in the console i have 2 errors: Uncaught SyntaxError: Cannot use import statement outside a module Inline Babel script:2 Uncaught ReferenceError: App is not defined I tried fixing the first one with the module if i insert the type: <script type="module" src="../../front-to-the-end/src/App.js"></script> but then i get another error - Uncaught SyntaxError: Unexpected token '<' on the jsx in the App.js file
Just remove the import statements and export default statements from the file to correct the first error. 2nd error can probably be corrected by giving <script type="text/babel">. Try these and report back
i am new to React how do i export default statements (My app.js has export default App) - Could the problem be coming from the fact that my Spring application is on port 8080 and react is on 3000? And on the second problem the script has type="text/babel" already - can i just remove it, you said it was not mandatory
I don't understand what you are trying to explain. Can you post a screenshot of your code and errors? That might be helpful
Did you eventually fix the problem? Are you running your frontend on a separate port and the spring on a separate port?

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.