I am building a custom Gutenberg block that makes a request to the WordPress REST API to get some Posts. I'm using axios
to make the request to the REST endpoint.
When the result comes back, there is an array of Post objects, and I can see the titles of the Posts, but they are all contained in the JSON object as title.rendered
and contain HTML entities eg.
title: {
rendered: "This has a hyphen – oh dear"
}`
I'm trying to populate a <SelectControl>
with the resulting data, so there's no way to use the React dangerouslySetInnerHTML
method which would solve the entities problem. So how can I get rid of these entities when populating the options?
Here is the code I'm using to populate the options from the REST response:
const options = response.data.map((post) => {
return {
label: post.title.rendered,
value: post.id,
};
});
import {decodeEntities} from "@wordpress/html-entities";
after runnpm install @wordpress/html-entities --save
– Thearchy