React native HTML entities
Asked Answered
A

3

17

I'm fetching some data in app from WordPress site successfully. Some entities like "&#8222" react native don't want to make like quotes and many more issues I have.

Is there some way to make HTML entities right in React Native App?

Thanks in advance.

Adenoidectomy answered 8/5, 2018 at 19:49 Comment(0)
M
19

You should be able to use something like html-entities to decode the text before rendering:

const entities = new Entities();
entities.decode('&#8222') // "„" (double low quotation mark)
Monotype answered 8/5, 2018 at 19:54 Comment(2)
I did install the package in my React Native project. I import using import Entities from 'html-entities' and put const entities = new Entities(); entities.decode(''); in render() { ... } but it returns 'undefined is not a constructor'. Could you explain the steps?Diversion
@JeafGilbert I think you're importing it in an incompatible way. See the README for html-entities for examples of how you can import what you need.Monotype
B
10

I had the same problem. This is the way I solved it:

First of all install html-entities :

npm install html-entities

In your code :

import {decode} from 'html-entities';

console.log(decode(stringToBeDecoded));
Baalbek answered 29/3, 2021 at 15:12 Comment(1)
For what it's worth to future readers, this is the one which worked for me. Importing Html5Entities as a constructor per the other answers didn't work for some reason.Velda
Q
4

Here's how to import and use a specific subpackage of Entities if the example in 'html-entities' README, which uses require(), won't work for you.

import {Html5Entities} from 'html-entities';

const entities = new Html5Entities();
entities.decode(stringToBeDecoded);
Quigley answered 2/10, 2020 at 21:14 Comment(1)
Import was helpful to a React Native noobie like me. For anyone else looking, install the packet using npm install html-entities or yarn add html-entitiesDarbies

© 2022 - 2024 — McMap. All rights reserved.