1

In my project, I'm fetching a json response from server which is an html content.

I've used react-native-render-html to render the html.But the table contents are not displaying.

Is there any method to display html table in react native? How could that be possible? Any help would be really appreciable. Please help.I've tried different methods such as react-native-htmlview.Nothing seems to be working as expected Following is my code for rendering html content using react-native-render-html.

json

    [{
    data: {
        course: {
            id: 2864,
            name: "2. This is the title",

            instructor: {
                sub: ""
            },
            menu_order: 0
        },
        description: "<p style="
        text - align: justify;
        ">India is undergoing an economic, social and technological transformation. Perhaps no other phase is going to be as critical as the present one in shaping the future of the country as well as determining the welfare of the people. The economic transition is the vital ingredient of these overall change. Faster growth accompanied by industrial sector expansion, skill addition to the people, creation of quality infrastructure etc. will fuel this growth phase.</p> <p style="
        text - align: justify;
        ">For understanding how important is the present development phase, we have to adopt a historical and comparative study. Following factors helps us to understand the present development phase of India.</p> <ol style="
        text - align: justify;
        "> <li><strong>Achievement of higher growth rate as a middle-income economy. </strong></li> </ol> <p style="
        text - align: justify;
        ">India at present is a lower middle-income economy and has to become a high-income economy undergoing a rapid growth phase extending at least three decades.</p> <p style="
        text - align: justify;
        ">Most important narrative about the Indian economy is that it is the third largest in the world in terms of Purchasing Power Parity GDP. But a superior way to asses a country’s development is to consider per capita income. The widely used ranking about countries’ economic position is that of the World Bank’s GDP Per capita (constant US $) and the data for 2016 shows that India’s per capita income is $1861 compared to China’s $ 6994.</p> <p style="
        text - align: justify;
        ">Table: Categorization of countries by World Bank</p> <table> <tbody> <tr> <td width="
        198 "><strong>Category</strong></td> <td width="
        318 "><strong>PCI as on 2015 in constant US Dollar</strong></td> </tr> <tr> <td width="
        198 ">Low Income Economy</td> <td width="
        318 ">$ 1025 or less</td> </tr> <tr> <td width="
        198 ">Middle Income Economy (India - $ 1861)</td> <td width="
        318 ">$1026 to $4035 (Lower Middle Income) $4036 to $12475 (Upper Middle Income)</td> </tr> <tr> <td width="
        198 ">Higher Income Economy</td> <td width="
        318 ">$ 12476 and above</td> </tr> </tbody> </table> <p style="
        text - align: justify;
        ">According to World Bank metrics, a country with less than $1045 is considered as low-income economy whereas one with a PCI of $12736 or higher is considered as a higher income economy. Higher income means higher standard of living. India is at the bottom of the lower middle-income economies and has to achieve higher economic growth, structural changes including industrialisation to raise per capita income near to the $12736 mark in the long run. This is what the country has to achieve through the transition.</p> <ol style="
        text - align: justify;
        " start="
        2 "> <li><strong> Industrial sector expansion</strong></li> </ol> <p style="
        text - align: justify;
        ">There are several factors that drives the economy to prosperity. Per capita income is just a monitored goal. How it can be raised is through achieving more productivity and employment generation in the sectors that can create big changes is decisive element for the country’s transition. Here comes the role of industrial sector. The industrial sector is known for generating huge employment with minimum skill addition. Similarly, no other sector has higher level of tradability as the industrial sector (means a country can earn big income through exports). Graduating to an expanded services sector without undergoing industrialisation will be self-defeating and unsuitable to a big economy like India. Depending on services sector for exports will not reward as other countries like to protect their services sector from the inflow of India’s skilled persons. If India can increase the contribution of the industrial sector in GDP from the present 30 per cent to say 40-45 %, it implies that sizable income and employment are created in the sector.</p> <ol style="
        text - align: justify;
        " start="
        3 "> <li><strong> Skilling the people when demography favours.</strong></li> </ol> <p style="
        text - align: justify;
        ">India has the largest number of young people in the world besides having largest workforce age group population. This situation is expected to remain till 2045. Now, youth means higher ability to produce, consume and thus stimulate overall economic activities. As in the case of an individual, better things happen for an economy when it is young.</p> <ol style="
        text - align: justify;
        curriculum: false,
        purchase_link: false
    },
    headers: [],
    status: 200
}]

I've pasted only few portion of my json response , just to show the html content.Also following is a little portion of my code just to how I'm doing rendering.Please help me to find a solution.

<Content>
      {this.state.section.map(article =>

         <CardItem>

         <HTML  html={article.data.description} imagesMaxWidth={Dimensions.get('window').width} />
         </CardItem>
           )}
        </Content>
Rohan Dhar
  • 1,827
  • 1
  • 10
  • 21
Linu Sherin
  • 1,712
  • 7
  • 38
  • 88
  • can you just try to render the below html , const htmlContent = `

    This HTML snippet is now rendered with native components !

    Enjoy a webview-free and blazing fast application

    Look at how happy this native cat is `; in your code
    – Learner Dec 12 '18 at 05:14
  • are you able to render the static html. also please post the version of the react-native-render-html – Learner Dec 12 '18 at 05:15
  • This how the output looks like when I try to render static html content https://i.stack.imgur.com/9DXqx.png . Also the version is `react-native-render-html": "^3.10.0` – Linu Sherin Dec 12 '18 at 05:19
  • so with static htmlContent its giving the proper output isn't – Learner Dec 12 '18 at 05:25
  • Yeah but `table` cannot be rendered. In my case all the other content expect `table` is coming – Linu Sherin Dec 12 '18 at 05:35
  • to the static html content can you add a simple html table , add this html from w3 schools, https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro – Learner Dec 12 '18 at 05:37
  • check whether it is rendering or not – Learner Dec 12 '18 at 05:38
  • No only the title is showing. Table and its contents are not rendered – Linu Sherin Dec 12 '18 at 05:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185103/discussion-between-dileep-thomas-and-anu). – Learner Dec 12 '18 at 05:42
  • so the th is rendering no the td element isn't it – Learner Dec 12 '18 at 06:05
  • No that is `H2` tag not `th` – Linu Sherin Dec 12 '18 at 06:06
  • please post the static html with the table – Learner Dec 12 '18 at 06:08
  • `

    HTML Table

    Company Contact Country
    Alfreds Futterkiste Maria Anders Germany
    Centro comercial Moctezuma Francisco Chang Mexico

    ` This is the html content of table

    – Linu Sherin Dec 12 '18 at 06:13
  • the package doesn't have the feature till now, there is an open issue. You need to do a work around for it – Learner Dec 12 '18 at 06:18
  • Is there any other way to do that? – Linu Sherin Dec 12 '18 at 06:21
  • you can check this link https://github.com/archriss/react-native-render-html/issues/43. i will post some work around solution posted by them. But you need to check and let me know. – Learner Dec 12 '18 at 06:23
  • i have posted a solution , check with the staticHtml for first, let me know what's the result – Learner Dec 12 '18 at 06:28

3 Answers3

0

try with this below snippet, basically all the table tags are replaced to iframe.

let staticHtml = `<h1>This HTML snippet is now rendered with native components !</h1> <h2>Enjoy a webview-free and blazing fast application</h2> <img src="i.imgur.com/dHLmxfO.jpg?2" /> <em style="textAlign: center;">Look at how happy this native cat is</em> <h2>HTML Table</h2> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr></table> `;
  //
  let content = staticHtml;
  const tables = staticHtml.match(/(<table(?:.|\n)*?<\/table>)/g);
  
  tables.map((table) => {
    content = content.replace(table, `<iframe srcdoc="${table}"></iframe>`);
  });

console.log("content after including iframe =>", content)
Learner
  • 8,379
  • 7
  • 44
  • 82
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/185132/discussion-on-answer-by-dileep-thomas-how-to-render-html-table-in-react-native). – Samuel Liew Dec 12 '18 at 12:09
  • Actually there is nothing like that.I've tried this https://www.npmjs.com/package/react-native-table-component.But it won't work in my case. Because I'm fetching response from server, which contains contents other than table. – Linu Sherin Dec 12 '18 at 12:09
  • I hope its not better to add our discussion on coments check on the chat link posted by Samuel liew – Learner Dec 12 '18 at 12:47
  • I have posted the package name on the chat discussion you can check that – Learner Dec 12 '18 at 12:57
0

You can partially do that with react-native-render-html library.

Examine the answer on following url, it solved my problem.

https://github.com/archriss/react-native-render-html/issues/43#issuecomment-426698839

Fatih Turgut
  • 319
  • 3
  • 9
0

Recommended Method

You can use @native-html/table-plugin

Not Recommended Method

Or use inline styles for every single cell in the html content

<td style="border: 1px solid black">Hashan</th>
Hashan Shalitha
  • 835
  • 8
  • 15