Monday, November 5, 2018

Rendering Raw HTML in your React Native application

This tutorial explains how to render raw HTML code in react native application using WebView component. Basically WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.
In this example we are going to Rendering raw HTML code in android application through WebView component, it does need to be manually or pro-grammatically sized, not something natively taken care of by the component. Additionally, styling the contents of the view, to better align with the rest of the UI would require similar solution, passing style sheets into the WebView container or even worse, having style attributes within the HTML itself.
NOTE : In this example we are using source prop of WebView component to display raw HTML code in android or ios screen.


Render Raw HTML Code in React Native Application Using WebView:

Lets follow the below steps to display raw HTML code in android or ios screen using WebView in React Native.



Step-1: Create a new react native project, if you don’t know how to create a new project in react native just follow this tutorial.

Step-2: Open index.android.js  / index.ios.js  in your favourite code editor and erase all code and follow this tutorial.

Step-3: Through react , react-native  packages import all required components.

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, WebView, } from 'react-native';

Step-4: Implement render method and place below layout design inside the render block.
  • htmlContent : Inside the render block,  we have created variable named as htmlContent, in which we are keeping raw HTML code.
  • Also we have placed the WebView component inside the render block, this will display the raw HTML code in android or ios screen. With the help of source prop, we are mapping raw HTML code with WebView Component. For example :
<WebView
  style={styles.container}
  javaScriptEnabled={true}
  domStorageEnabled={true}
  source={{ html: htmlContent  }}
 />

Step-5 : Apply the below style sheet design.
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#e5e5e5",
  },

});

Complete Source Code for App.js 

Lets see the complete source code that helps to render raw HTML code  in react native application using webview.
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, WebView, } from 'react-native';


export default class App extends Component {

  render() {

    var htmlContent = '<h1 style="color:red;"> Skptricks Blog </h1>' +
    '<h3> The Basics Of ES6 Generators - Javascript</h3>' +
    '<p> With ES6 generators, we have a different kind of function, which may be paused in the middle, one or many times, and resumed later, allowin... </p>' +
    '<img src="https://3.bp.blogspot.com/-XhR2CaeJE3M/W8oKAFPha_I/AAAAAAAACCs/MnKDQ_jtCjg8kgF9lirPAfa0CupLcsWhQCLcBGAs/s600/gene.png" alt="Image" width="90%" height="200" >' +

    '<h3> React Native Picker Spinner DropDown Menu List Example - Android</h3>' +
    '<p> In this tutorial, we are going to discuss how to create simple picker s pinner dropdown menu list in react native application . Picker Spi... </p>' + '<img src="https://4.bp.blogspot.com/-puNk3kY_CaQ/W8yOhWzPiOI/AAAAAAAACDU/VlQ9bzG3x6c_7Vfm8ENo2c3uQh1jvhd0ACLcBGAs/s600/picker.png" alt="Image" width="90%" height="200" >' +

    '<h3> Create Custom Snackbar Component Example In React Native- Android</h3>' +
    '<p> This tutorial explains how to create simple Snackbar component in react native application . Snackbars are just like Toast messages except ... </p>' +
    '<img src="https://2.bp.blogspot.com/-93jY0dxBGR4/W8NnVz92VSI/AAAAAAAACBc/q77WAtv-Xb0LGsSOBxGRrU1d1QXyYVT6ACLcBGAs/s600/snack.png" alt="Image" width="90%" height="200" >' +

    '<h3> React Native Simple Custom GridView Layout Example Android</h3>' +
    '<p> This tutorial explains how to create simple custom grid view in react native application.  Grid View that displays items in a two-dimension... </p>' +
    '<img src="https://1.bp.blogspot.com/-U6DxHFlg770/W7ECe9uy4rI/AAAAAAAAB-0/Mumx0xM2APQkq4ynVni17BEo1wK1tU-NgCLcBGAs/s600/grid.png" alt="Image" width="90%" height="200" >';

    return (
        <WebView
        style={styles.container}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        source={{ html: htmlContent  }}
        />
      );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#e5e5e5",
  },

});

Screenshot :

Rendering Raw HTML in your React Native application

Rendering Raw HTML in your React Native application

Download Link : 
https://github.com/skptricks/React-Native/tree/master/Rendering%20Raw%20HTML%20in%20your%20React%20Native%20application

This is all about rendering Raw HTML in your React Native application. It does a fantastic job of rendering the HTML natively in the react native layout, with the option to pass in a standard react native styles object referencing standard HTML elements. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.


No comments:

Post a Comment