84

In my React Native app, I am pulling in JSON data that has raw HTML elements like this: <p>This is some text. Let&#8217;s figure out...</p>

I've added the data to a view in my app like this:

<Text>{this.props.content}</Text>

The problem is that the HTML comes out raw, it does not render like it would in a browser. Is there a way to get my JSON data to look like it would in a browser, inside my app view?

4

9 Answers 9

107

Edit Jan 2021: The React Native docs currently recommend React Native WebView:

<WebView
    originWhitelist={['*']}
    source={{ html: '<p>Here I am</p>' }}
/>

https://github.com/react-native-webview/react-native-webview

If you don't want to embed a WebView, there are also third party libraries to render HTML into native views:

  1. react-native-render-html
  2. react-native-htmlview

Edit March 2017: the html prop has been deprecated. Use source instead:

<WebView source={{html: '<p>Here I am</p>'}} />

https://facebook.github.io/react-native/docs/webview.html#html

Thanks to Justin for pointing this out.


Edit Feb 2017: the PR was accepted a while back, so to render HTML in React Native, simply:

<WebView html={'<p>Here I am</p>'} />

Original Answer:

I don't think this is currently possible. The behavior you're seeing is expected, since the Text component only outputs... well, text. You need another component that outputs HTML - and that's the WebView.

Unfortunately right now there's no way of just directly setting the HTML on this component:

https://github.com/facebook/react-native/issues/506

However I've just created this PR which implements a basic version of this feature so hopefully it'll land in some form soonish.

Sign up to request clarification or add additional context in comments.

6 Comments

Awesome Colin, thanks for doing that! I'll keep an eye on the github issue.
According to the docs at versions 0.42 (facebook.github.io/react-native/docs/webview.html#html) the html property has been deprecated. It now says that it should be used with the source prop instead. <WebView source={{ html: '<p>Here I am</p>' }} />.
Docs now say Note that static HTML will require setting originWhitelist to ["*"]. Although I can't get this to work.
Man this looks great, except I pasted <WebView source={{html: '<p>Here I am</p>'}} /> into my component and it's not rendering anything! Help! (I'll try react-native-render-html in the meantime)
|
17

I found this component. https://github.com/jsdf/react-native-htmlview

This component takes HTML content and renders it as native views, with customisable style and handling of links, etc.

7 Comments

HTML styling with CSS does not work with react-native-htmlview. If that's an issue, I don't recommend it.
Don't recommend it too.
It is now 2019 and I see no modern, recently updated, useable option for this!
@RenatoBack If you need inline CSS support, see stackoverflow.com/a/54899575/2779871
@RenatoBack "HTML styling with CSS does not work with react-native-htmlview." (Sep 6 '16 at 22:56)
|
16

A pure JavaScript react-native component that renders your HTML into 100% native views. It's made to be extremely customizable and easy to use and aims at being able to render anything you throw at it.

react-native-render-html

Using this component will improve your application memory footprint and performance when compared to embedded WebViews.

Install

npm install react-native-render-html --save or yarn add react-native-render-html

Basic usage

import React from "react";
import { ScrollView, useWindowDimensions } from "react-native";
import RenderHTML from "react-native-render-html";

const html = `
  <h1>This HTML snippet is now rendered with native components !</h1>
  <h2>Enjoy a webview-free and blazing fast application</h2>
  <img src="https://i.imgur.com/dHLmxfO.jpg?2" />
  <em style="textAlign: center;">Look at how happy this native cat is</em>
`;

export default function App() {
  // Allow images to scale to available width
  // with contentWidth prop.
  const { width } = useWindowDimensions();
  return (
    <ScrollView style={{ flex: 1 }}>
      <RenderHTML contentWidth={width} source={{ html }} />
    </ScrollView>
  );
}

RenderHTML Props reference

You may customize the style of elements via class names, tags, and you can even register custom renders for tags. More info on the official website.

1 Comment

I think that as of version 6.0.0 of react-native-render-html the line should actually be: <RenderHTML contentWidth={width} source={{ html : html }} />
3

i uses Js function replace simply.

<Text>{item.excerpt.rendered.replace(/<\/?[^>]+(>|$)/g, "")}</Text>

2 Comments

Could you add some information about what this does or how / why it works?
This wont render HTML it just strips out any html content.
2

React Native has updated the WebView component to allow for direct html rendering. Here's an example that works for me

var htmlCode = "<b>I am rendered in a <i>WebView</i></b>";

<WebView
  ref={'webview'}
  automaticallyAdjustContentInsets={false}
  style={styles.webView}
  html={htmlCode} />

1 Comment

The html prop is now deprecated. Use the source prop instead.
2
 <WebView ref={'webview'} automaticallyAdjustContentInsets={false} source={require('../Assets/aboutus.html')} />

This worked for me :) I have html text aboutus file.

Comments

2
import HTML from "react-native-render-html";
var htmlCode = "<b>I am <i>Italic</i></b>";


<HTML source={{html: htmlCode}}/>

enter image description here

Comments

1

The WebView component was not rendering for me HTML snippets, like

<b>hello</b>, world!

But if I would enclose the HTML snippet in a document, like the example below, then it did actually render the document:

<View style={styles.accContent}>
    <WebView source={{html: `<!DOCTYPE html><html><body style="font-size: 3rem">${data.content}</body></html>`}} />
</View> 

Comments

0

react-native-htmlview package works for me

Adding to @sjoonk answer.

  • WebView didn't work for me. The app crashed without an error message if I use this. May be there was a version mismatch.
  • react-native-render-html also didn't work as I was working in .tsx file. I couldn't download @types/react-native-render-html package and there was an error.

The only thing that worked for me was https://www.npmjs.com/package/react-native-htmlview

I was using yarn and typescript. Did the following to install the package.

yarn add react-native-htmlview --save
yarn add @types/react-native-htmlview --save-dev

In .tsx component

import HTMLView from 'react-native-htmlview';

function App(){

    const htmlContent = `<p><a href="http://jsdf.co">&hearts; nice job!</a></p>`;

    return (
      <HTMLView value={htmlContent}/>
    );

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.