Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML tags to JSX React Native

I'm doing a call to an API that returns some HTML:

example:

<p>Hello there</p><p>Javascripti</p>

I want to use this in my React Native app but this time I want to replace all the <p> tags for <Text> because that is something I can use in React Native.

How can I achieve something like this? I can't find out how to translate or parse HTML.

EDIT: I used Regex to make the <p> a <Text> but it just returns a string. It displays this on the screen: <Text>Lorem Ipsum</Text. How can I parse this to readable JSX?

like image 626
Nieck Avatar asked May 03 '26 11:05

Nieck


1 Answers

JSX is basically syntax sugar (as seen here, here and especially here) for calling createElement:

React.createElement(component, props, ...children)

So once you've captured the text inside the <p></p> tags you can generate any tag like this:

let capturedContent = 'Hello there'

const element = React.createElement('Text', { }, capturedContent)
like image 92
Mauricio Pasquier Juan Avatar answered May 06 '26 01:05

Mauricio Pasquier Juan