Replace dangerouslySetInnerHTML
with createInterpolateElement
in React apps
#6759
Labels
Changelogged
Whether the issue/PR has been added to release notes.
Enhancement
New feature or improvement of an existing one
Infrastructure
Changes impacting testing infrastructure or build tooling
javascript
Pull requests that update Javascript code
P2
Low priority
Milestone
Feature Description
In React components we are often using a combination of
dangerouslySetInnerHTML
(or<RawHTML>
) with one of the i18n functions like__()
for rendering translated strings containing HTML elements like:This approach has some limitations. One is that only simple HTML elements can be interpolated this way. E.g. it's not possible to interpolate a React component into a translated string:
The second issue is that using
dangerouslySetInnerHTML
along with translated strings has some security concerns.However, since WordPress 5.5 there's a solution available at our disposal:
createInterpolateElement
.Here's how the same example would look like with
createInterpolateElement
:We should replace all occurrences of
dangerouslySetInnerHTML
or<RawHTML>
withcreateInterpolateElement
.Acceptance Criteria
No response
Implementation Brief
No response
QA Testing Instructions
No response
Demo
No response
Changelog Entry
No response
The text was updated successfully, but these errors were encountered: