DEV Community

loading...

Discussion on: 5 Canny little tricks for React devs

Collapse
michalhonc profile image
michalhonc Author • Edited

The main premise is that JSX compiler parses HTML entities, but React does NOT. This inconsistent behavior is easy source of bugs. When you pass plain string as prop with value ©, it works as expected, ie. html char of copyright symbol is present. The JSX compiler parses that as expected. BUT value of {'&' + 'copy' + ';'} will result in plain string ©. As precaution I always use String.fromCharCode so I don't have to think if React parses it or it's on JSX compiler.

const copySymbol = '©';
<Button symbol={copySymbol} /> // results in '&copy;'

<Button symbol={'&' + 'copy' + ';'} /> // results in '&copy;'

<Button symbol="&copy;" /> // results in '©'

<Button symbol={String.fromCharCode(169)} /> // results in '©'

const copySymbolFromCharCode = String.fromCharCode(169)
<Button symbol={copySymbolFromCharCode} /> // results in '©'