question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<div> contains <img>,Can't display images

See original GitHub issue

“react-native”: “0.48.3” “react-native-htmlview”: “^0.12.0” android:7.0

here is my code:

render() {
    const htmlContent = `
        <div>
            <img src="http://d.lanrentuku.com/down/png/1504/bianping-yuanicon/bianping-yuanicon-09.png" />
        </div>
    `;
    return (
        <View>
            <HTMLView
                value={htmlContent}
            />
        </View>
    );
}

If use <div> containing < img>, will not be able to display the image.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9

github_iconTop GitHub Comments

19reactions
keseparacommented, Oct 9, 2017

You need to add another function named renderNode():

renderNode(node, index, siblings, parent, defaultRenderer) {
        if (node.name == 'img') {
            const a = node.attribs;
            return ( <Image style={{width: 300, height: 300}} source={{uri: a.src}}/> );
        }
    }

and then change HTMLView tag like this:

<HTMLView value={htmlContent} renderNode={this.renderNode} />

That’s all. Your renderNode() function and render() function should be siblings at that example. @Ctonys

4reactions
WajeehZantoutcommented, Feb 20, 2018

Hello guys, this is my code. I hope it can help you

import HTMLView from 'react-native-htmlview';
import React, { Component } from 'react';
import { Image, Dimensions, PixelRatio } from 'react-native';

const { width } = Dimensions.get('window');

export default class HtmlView extends Component {
  renderNode(node, index, siblings, parent, defaultRenderer) {
    if (node.name == 'img') {
      const { src, height } = node.attribs;
      const imageHeight = height || 300;
      return (
        <Image
          key={index}
          style={{ width: width * PixelRatio.get(), height: imageHeight * PixelRatio.get() }}
          source={{ uri: src }} />
      );
    }
  }

  render() {
    return (
      <HTMLView
        value={`<html><body>${this.props.content}</body></html>`}
        renderNode={this.renderNode}
        stylesheet={this.props.stylesheet} />
    )
  }
}

PixelRatio.get() returns the device pixel density.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image inside div doesn't show - Stack Overflow
How to fix it: To fix this, you need to set your width and height to actual values, so that there is an...
Read more >
The IMG (Image) Element
Browsers that cannot display in-line images ignore the IMG element unless it contains the ALT attribute. Note that some browsers can display (or...
Read more >
Using the image tag in React - Dave Ceddia
How do you refer to an image in React? The img tag is a bit weird. Learn how to include images in your...
Read more >
Responsive images - Learn web development | MDN
The content images have been set so that if the body element becomes smaller than the image, the images start to shrink so...
Read more >
How to fix CSS background-image not working | HTML/CSS
If there is no HTML content in the element, it might have zero width or height (or both!). This means that even though...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found