Use UTF-8 characters directly
版权 ©
Use HTML escape characters
版权 ©
or Decimal escape characters
版权 ©
But if you add a layer of curly brackets outside, react will use the escaped characters to prevent xss Character entities are escaped again
React will escape all strings to be displayed to the DOM to prevent XSS. Therefore, if JSX contains escaped entity characters, such as © (©), it will not be displayed correctly in the final DOM because React automatically escapes the special characters in ©.
{'版权 ©'}
Error output
版权 ©
Correct writing:
Using UTF-8 characters directly can still output correctly
{'版权 ©'}
The safe way is to use the corresponding Unicode code
{'版权 \u00a9'}
Use fromCharCode
{'版权 ' + String.fromCharCode(169)}
Use array assembly
{['版权 ', ©]}
Use dangerouslySetInnerHTML to avoid React escape characters
JSX Gotchas
Deep into the react technology stack
Use UTF-8 characters directly
版权 ©
Use HTML escape characters
版权 ©
or decimal escape characters
版权 ©
But if you add a layer of braces outside, react will escape the escaped character entity again in order to prevent xss
React will escape all strings to be displayed in the DOM to prevent XSS. Therefore, if JSX contains escaped entity characters, such as © (©), it will not be displayed correctly in the final DOM because React automatically escapes the special characters in ©.
{'版权 ©'}
Error output
版权 ©
Correct writing:
Using UTF-8 characters directly can still output correctly
{'版权 ©'}
The safe way is to use the corresponding Unicode code
{'版权 \u00a9'}
Use fromCharCode
{'版权 ' + String.fromCharCode(169)}
Use array assembly
{['版权 ', ©]}
Use dangerouslySetInnerHTML to avoid React escape characters
The above content is the HTML escape writing method in React, I hope it can help everyone.
Related recommendations:
What are the ways to write components in React
The difference between setState in React and Preact
The above is the detailed content of HTML escaping in React. For more information, please follow other related articles on the PHP Chinese website!