Hello Friends 👋,
Welcome To Infinitbility! ❤️
React native render HTML provides the feature to render anchor tag or link in your HTML content but you want the link is clickable then you have to some extra stuff.
We know when we want to open any link from react native app we use react native linking to redirect the app to the browser and open the URL.
So, we have to do the same thing here also for open URLs or you want to click on the link.
Follow the below steps to make your HTML link clickable.
Step 1: Add on link press props
React native render HTML provides props onLinkPress
to handle link.
Add these props to your HTML component
<HTML
...
onLinkPress={(evt, href) => { console.log(href) }}
...
/>
Step 2: use linking with render HTML
- import linking from react native
import { Linking } from 'react-native';
<HTML
...
onLinkPress={(evt, href) => { Linking.openURL(href) }}
...
/>
Step 3: Complete Example to implement working link
import React, { Component } from "react";
import { ScrollView, useWindowDimensions, Linking } from "react-native";
import HTML from "react-native-render-html";
const htmlContent = `
<h1>This HTML snippet is now rendered with native components !</h1>
<h2>Enjoy a webview-free and blazing fast application</h2>
<a href="https://infinitbility.github.io" >infinitbility.github.io</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 Demo() {
const contentWidth = useWindowDimensions().width;
return (
<ScrollView style={{ flex: 1 }}>
<HTML
source={{ html: htmlContent }}
contentWidth={contentWidth}
onLinkPress={(evt, href) => { Linking.openURL(href) }}
/>
</ScrollView>
);
}
Thanks for reading…
More From React Native Tutorial
Basics
1. Introduction To React Native
2. React Native Environment Setup using expo
3. React Native Environment Setup for windows
4. React Native Environment setup on Mac OS
5. React Native Environment setup on linux
6. React Native Project Structure
Advances
4. React Native DatepickerAndroid
5. React native ScrollView scroll to position
6. How to align icon with text in react native
8. React Native Firebase Crashlytics
Error & Issue Solution
1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
2. Expiring Daemon because JVM heap space is exhausted In React Native
3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
5. App crashed immediately after install react native video or track player
6. how to delete SQLite database in android react native
7. React native material dropdown twice click issue
8. How to get the current route in react-navigation?
9. how to disable drawer on the drawer navigation screen?
10. Image not showing in ios 14 react native
11. React Native image picker launchimagelibrary on second time issue
12. how to open any link from react native render Html