reactjs html rendering as string not html

render string as html
react render html without dangerouslysetinnerhtml
react render html string from server
react render html file
react not rendering html
html tags rendered as string
react fragment html string
how to use html in react js

Trying to render html but I'm getting strings

var ref = firebase.database().ref('raffle/');
ref.on('value', (snapshot) => {
  var content = ``;
  var IDwrapper = document.getElementById('raffleFeed');

  snapshot.forEach((data) => {
    // var rImage = data.val().raffleImage;
    var rTitle = data.val().raffleTitle;
    var rAmount = data.val().raffleAmount;
    var rDescription = data.val().raffleDescription;
    var rEntries = data.val().raffleEntries;
    var rButton = '<button class="btn btn-secondary"> Button </button>';

    console.log(data.val());
    // content += '<Row>';
    content += '<Col sm="4">';
    content += '<CardBody>';

    content += '<CardTitle>' + rTitle + '</CardTitle>';
    content += '<CardText>' + rAmount + '</CardText>';
    content += '<CardText>' + rEntries + '</CardText>';
    content += '<CardText>' + rDescription + '</CardText>';
    content += rButton;

    content += '</CardBody>';
    content += '</Col>';
    // content += '</Row>'; //end
  });

  IDwrapper.append(content);

the following is rendered

<Col sm="4"><CardBody><CardTitle>1</CardTitle><CardText>1</CardText><CardText>1</CardText><CardText>1</CardText><button class="btn btn-secondary"> Button </button></CardBody></Col><Col sm="4"><CardBody><CardTitle>2</CardTitle><CardText>2</CardText><CardText>2</CardText><CardText>2</CardText><button class="btn btn-secondary"> Button </button></CardBody></Col><Col sm="4"><CardBody><CardTitle>3</CardTitle><CardText>3</CardText><CardText>3</CardText><CardText>3</CardText><button class="btn btn-secondary"> Button </button></CardBody></Col><Col sm="4"><CardBody><CardTitle>4</CardTitle><CardText>4</CardText><CardText>4</CardText><CardText>4</CardText><button class="btn btn-secondary"> Button </button></CardBody></Col><Col sm="4"><CardBody><CardTitle>5</CardTitle><CardText>5</CardText><CardText>5</CardText><CardText>5</CardText><button class="btn btn-secondary"> Button </button></CardBody></Col>

not sure what I'm overlooking but its driving me insane. thanks for the help

You can push your JSX into an array, or use a React Fragment, or worst case scenario, use dangerouslySetInnerHtml.

Here's a simple example of using mixed array content (strings plus JSX): https://codesandbox.io/s/1v1xmq1kmq

Same example as above, however, instead of using an array, just wrapping the content within a Fragment: https://codesandbox.io/s/92r12m7zp

Here's a more complex example using a chunked array (change the columns to a number that divides into 24 evenly): https://codesandbox.io/s/30v7qvoz3m

Example of using an array:

const content = [];

snapshot.forEach((data) => {
  const rTitle = data.val().raffleTitle;
  const rAmount = data.val().raffleAmount;
  const rDescription = data.val().raffleDescription;
  const rEntries = data.val().raffleEntries;
  const rButton = <button class="btn btn-secondary"> Button </button>

  const jsxContent = (
    <Row key={rTitle}>
      <Col sm="4">
        <CardBody>
          <CardTitle> { rTitle }</CardTitle>
           ...etc
        <CardBody>
      </Col>
    </Row>
  )

  content.push(jsxContent);
});

Then in your React component:

<div>
 {content}
</div>

Usually the database will store a JSON array of raffle ticket data like so:

[ 
  { 
    id: xxxx-xxxx-xxxx-xxx (unique uuid), 
    title: "Example Raffle Title", 
    amount: 10.00, 
    description: "Example raffle description", 
    entries: 49, 
    maxEntries: 500 
  }, 
  { 
    id: xxxx-xxxx-xxxx-xxx,
    title: "Example Raffle Title 2",  
    ...etc 
  }, 
  {
    ...etc
  } 
] 

Then you'll retrieve this JSON array from your database, map over the array, display each item, and apply a unique id to an element's onClick handler (click on one of the tickets to retrieve the id, which then can be used to charge the customer, update the database entries, ...etc).

Working example: https://codesandbox.io/s/8446420yn2

React rendering raw HTML code instead of interpreting it , Any ideas why the string returned from my function is not showing as intended? Render HTML string as real HTML in a React component. How to render a react element using an html string? [duplicate] render html in state string [duplicate] javascript reactjs.

content is just a string, but you need a node.

If it is an option, you may try this

 IDwrapper.innerHTML = content;

How to render HTML in React, Find out how to render an HTML string in the DOM without escaping, HTML is not escaped at all in this case, and it might cause XSS issues. The string is not printed in the the textarea. I tried this to debug: render() { var summary = "this is a summary"; return (// .. shortened for brevity <textarea className="form-control" rows="22" ref="summaryContent"> {summary} </textarea> ..); } the summary string printed correctly inside the mutable textearea.

create an object var cardValues = {} and assign the various snapshot values to this object, e.g. cardValues.title = snapshot.val().title. Then, pass this object to a card builder function that uses document.createElement and .appendChild methods to render out your cards.

JSX In Depth – React, React thinks <hello /> is an HTML tag because it's not capitalized: return <hello This often comes up when you want to render a different component based on a prop: When you pass a string literal, its value is HTML-unescaped. So these  Render a React element to its initial HTML. React will return an HTML string. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.

ReactDOMServer – React, The ReactDOMServer object enables you to render components to static markup. If you plan to use React on the client to make the markup interactive, do not use this method. Returns a Readable stream that outputs an HTML string. Not for rendering some raw html provided via an API, for example. Prior to the Fragment API it was always a pain for me, that required additional span wraps that would sometimes mess with flex layouts. When I stumbled upon this question looking for a possible solution I thought I'd share how I got around things.

Inserting Raw HTML · Issue #527 · reactjs/react-rails · GitHub, I am trying to print raw html in the render method. To render markup from string in React you must use this from Google since it is one of the first results for how to render raw HTML in React, the above syntax is not correct. Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like if or the conditional operator to create elements representing the current state, and let React update the UI to match them.

How to render HTML string in React - UI Galaxy, Sometimes you may want to render an HTML string when using React. This string may come from WYSIWYG editors. But there is a problem  Elements are the smallest building blocks of React apps. An element describes what you want to see on the screen: Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements. One might confuse elements with a more widely known concept of

Comments
  • This seems to be achievable easily using react only, your HTML can be rendred using jsx
  • @Justcode can you show me in code? im confused somewhere
  • Explain what you want to achieve create small demo
  • just trying to loop thru my firebase database and render the values to the view @Justcode
  • Can you create demo on stackblitz.com?
  • is this the traditional react way of displaying database nodes to your view?
  • so how would i append but render the HTML tags with my object variables values?
  • If you are using REACT.JS, you need to return() all that through reacts render()` instead of directly appending to DOM. Everything you want to be reactive, must go through react.