score:29
There is no sane workaround.
You are getting this error because Thymeleaf outputs XML, and JSX parsers do not parse XML.
You did this because JSX looks very, very similar to XML. But they are very, very different, and even if you somehow hacked Thymeleaf to strip namespaced attributes and managed to get a component to render, it would be merely a fleeting moment of duct-taped-together, jury-rigged code that will fall apart under further use.
This is a really, really bad idea because JSX is Javascript. You are generating Javascript on the fly. Just to name a few reasons this will not work in the long term:
- This makes your components difficult if not impossible to test.
- Reasoning about application state will be a nightmare as you will struggle to figure out if the source of a certain state is coming from Thymeleaf or JS.
- Your application will completely grind to a halt if Thymeleaf outputs bad JS.
- These problems will all get worse with time (Thyme?) as as developers abuse the ease with which they can render server-side data to the client-side, leading to an insane application architecture.
Do not do this. Just use Thymeleaf, or just use React.
Sample Alternative: I primarily work on a React application backed by a Java backend. So I understand how someone could stumble upon this hybrid and think it might be a good idea. You are likely already using Thymeleaf and are trying to figure out how you can avoid rewriting your servlets but still get the power of React.
We were in a similar boat two years ago, except with an aging JSP frontend, but the difference is negligible. What we did (and it works well) is use a JSP page to bootstrap the entire React application. There is now one JSP page that we render to the user. This JSP page outputs JSON into a single <script>
tag that contains some initial startup data that we would otherwise have to fetch immediately. This contains resources, properties, and just plain data.
We then output another <script>
that points to the location of a compiled JS module containing the entire standalone React application. This application loads the JSON data once when it starts up and then makes backend calls for the rest. In some places, we have to use JSP for these, which is less than ideal but still better than your solution. What we do is have the JSP pages output a single attribute containing JSON. In this way (and with some careful pruning by our XHR library) we get a poor man's data interchange layer built atop a JSP framework we don't have time to change.
It is definitely not ideal, but it works well and we have benefited vastly from the many advantages of React. When we do have issues with this peculiar implementation, they are easy to isolate and resolve.
score:2
Now you can use data- prefix attributes (ex. data-th-text="${message}"
).
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#support-for-html5-friendly-attribute-and-element-names
score:5
It is possible wrap ReactJS apps in Thymeleaf. Think if you want a static persistent part (like some links, or even just displayed data), you could use Thymeleaf. If you have a complicated part (something that requires DOM repaints, shared data, updates from UI/Sockets/whatever), you could use React.
If you need to pass state you could use Redux/other methods.
You could have your backend send data via a rest API to the React part and just render your simple parts as fragments or as whole chunks of plain HTML using Thymeleaf.
Remember, Thymeleaf is really just HTML. React is virtual DOM that renders as HTML. It's actually fairly easy to migrate one to the other. So you could write anything "Static" or that does not respond much to UI, in Thymeleaf/HTML. You could also just render those parts in React too, but without State.
Thymeleaf 3 allows you to render variables from your Java to a separate JS file. So that is also an option to pass into JSX
function showCode() {
var code = /*[[${code}]]*/ '12345';
document.getElementById('code').innerHTML = code;
}
Source: stackoverflow.com
Related Query
- How to use Thymeleaf th:text in reactJS
- How to use Redirect in version 5 of react-router-dom of Reactjs
- How to use JQuery with ReactJS
- How to properly use componentWillUnmount() in ReactJs
- How can I use passportjs and reactjs together?
- Reactjs how to use ref inside map function?
- How to use data toggle collapse in Reactjs with Bootstrap?
- How to use switch cases inside JSX: ReactJS
- how to use in Reactjs functional component history.push
- How to use drag and drop functionality both columns and rows in my React Table - ReactJS
- How do I use reactjs with Google DFP/AdSense
- How to use media print within Reactjs web app
- Reactjs promises, how should we use it?
- How can I use CSS @media for responsive with makeStyles on Reactjs Material UI?
- How to use a proxy for a link with reactjs
- How to use refresh token in reactjs
- How can I use Esri Arcgis Map in ReactJs Project?
- How to use a ReactJS Component with Clojurescipt/Reagent
- ReactJS + Material-UI: How to use Material-UI’s FlatButton and Dialog in each TableRow?
- How to use Django template in ReactJS
- How to use debounce in ReactJS
- How to use external js files in reactjs
- How to use environment variables in ReactJS imported CSS?
- ReactJs - how to use multiple children in a component
- How to use d3 with reactJS / nextJS?
- ReactJS Router- How to use history push within function component with parameters?
- How to use this highchart map in reactJS
- ReactJS How to use Router's props inside app.js file
- ReactJS How do I use state hooks for a list of text changing buttons with a timeout?
- How to use dynamic routing in ReactJS with a colon
More Query from same tag
- I have an object of fake data that I want to make copies of, how can I do that?
- How to tell useEffect to wait for data
- React js how to inject javascript in jsx object
- Getting duplicates when Incrementing & Decrementing using the Arrow keys with React JS
- How to avoid line wrap in a form generated with typescript?
- Mapping through data and creating input fields, but why can I type in all of them except one?
- How to add ref to CSVLINK in react typescript?
- Export default class and function from a React class
- TypeError: Failed to fetch with input text
- Using withWidth HOC from material UI
- Rendering Text Name as Component in ReactJS
- Keep left margin of a div inline with another div when resizing
- TypeError: w.map is not a function
- How to Handle Error using try catch using React State with Arrow functions
- Rename a key value that is part of the React state
- Redux saga not dispatching action on live sockets
- Is there a way to pass a group of props in react?
- Reactjs Typescript contact parameter problem
- Gatsby markdown images are stuck with blurred span
- Changing CSS Attribute on click in React while using Styled Components
- Text replacing in ReactJS
- How to use Private route in react-router-dom@v6
- Cannot access property passed via user provider
- TypeScript: Type 'T' does not satisfy the constraint '(...args: any) => any'
- React Hooks - useEffect fires even though the state did not change
- How to do routing in react by clicking a button in class component?
- Reading isLoggedIn from a file in React
- Access a form control value from component
- React button component asks for key props
- React passing function as prop