You can consider using the React attribute dangerouslySetInnerHTML:
class YourComponent{
render() {
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return (
<div className="Container" dangerouslySetInnerHTML={{__html: someHtml}}></div>
)
}
}
Answer from user8296051 on Stack OverflowSo i have a text, i need to convert it into a react component and render it ,
const [jsxCode, setJsxCode] = useState(\
<mesh><boxGeometry args={[1, 1, 1]} /><meshBasicMaterial color="red" /></mesh>`);`
I tried different things
-
using dangerouslySetInnerHTML inside the canvas, but that is not being allowed as only children or this is allowed
-
tried using a parser library not working
-
React.createElement(React.Fragment, {}, jsxCode);, this will have worked if jsxCode was not a string , but actual components
const [jsxCode, setJsxCode] = useState(<mesh><boxGeometry args={[1, 1, 1]} /><meshBasicMaterial color="red" /></mesh>);
Any pointers is deeply appreciated on conversion
))} ) }
Saving Script file to JSX - Adobe Photoshop - Creative COW
How to convert .txt file to JSON using React and JavaScript - Stack Overflow
convert an html file into jsx file
want to convert text to react compnent
What is JSX?
How accurate is the HTML to JSX conversion?
⏱️ How long does it take to converter JSX file?
You can consider using the React attribute dangerouslySetInnerHTML:
class YourComponent{
render() {
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return (
<div className="Container" dangerouslySetInnerHTML={{__html: someHtml}}></div>
)
}
}
2023 answer update
Seem react-html-parser has no longer been maintained for a few years already. As pointed on the comment by Bill Keller, there is a library still actively maintained named html-react-parser. However, please read carefully on the Readme and make some considerations before using it in production. Like in this section Is this XSS safe?, if it is acceptable, then you are good to go.
Another alternative sounds more promising to me because it emphasizes "Safely" on their repo and home page. The library is named "Interweave". Please check the following repo for more detail about installation and usage: https://github.com/milesj/interweave/
2018 answer
Personally, I love to do it just like in the previous answer which recommends the usage of dangerouslySetInnerHTML property in JSX.
Just for an alternative, nowadays there is a library called react-html-parser. You can check it and install from NPM registry at this URL: https://www.npmjs.com/package/react-html-parser. Today's weekly download statistic for that package is 23,696. Looks a quite popular library to use. Even it looks more convenient to use, my self, still need more read and further consideration before really using it.
Code snippet copied from the NPM page:
import React from 'react';
import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';
class HtmlComponent extends React.Component {
render() {
const html = '<div>Example HTML string</div>';
return <div>{ ReactHtmlParser(html) }</div>;
}
}
» npm install @madebyconnor/rich-text-to-jsx
Something like this would work, providing your txt file is consistent:
const data = `Title: Blazing Saddles
Release Year: 1974
Format: VHS
Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahn
Title: Casablanca
Release Year: 1942
Format: DVD
Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre
Title: Charade
Release Year: 1953
Format: DVD
Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy
Title: Cool Hand Luke
Release Year: 1967
Format: VHS
Stars: Paul Newman, George Kennedy, Strother Martin`
console.log(data.split('\n\n').map(entry => {
const obj = {}
entry.split('\n').forEach(keyValue => {
const split = keyValue.split(": ")
const key = split[0]
const value = split[1]
obj[key] = key === "Stars" ? value.split(", ") : value
})
return obj
}))
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This question was asked long time ago but I was looking for solution for similar problem recently and I found an easy way to deal with it. So, if someone is still interested, here it is.
Instead of using all these manipulations, you can simply use JSON.parse.
const convertToJSON = (data) => {
return data.split('\n\n').map(entry => {
return JSON.parse(entry);
});
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This solution works for me. I hope it works for you too.