JSX Entity Escaping Guide
Quick Reference
Always escape these characters in JSX text content:
Character | Escape Code | Example |
---|---|---|
' (apostrophe) |
' |
don't |
" (quote) |
" |
"hello" |
> (greater than) |
> |
5 > 3 |
} (closing brace) |
} |
object} |
✅ Correct Examples
// Apostrophes
<p>You don't have any messages</p>
<h2>Children's Privacy</h2>
// Quotes in text
<p>She said "Hello world"</p>
<p>GiveProtocol ("we," "our," or "us")</p>
// Greater than symbols
<p>Donations > $100 get special recognition</p>
// Closing braces in text
<p>Use this syntax: }</p>
❌ Incorrect Examples
// Will cause DeepSource JS-0454 errors
<p>You don't have any messages</p>
<h2>Children's Privacy</h2>
<p>She said "Hello world"</p>
<p>Donations > $100 get special recognition</p>
Alternative: Template Literals
For complex text with many special characters:
<p>{'You don\'t need to escape inside template literals'}</p>
<p>{`She said "Hello" and it's working > perfectly`}</p>
IDE Setup
VS Code Extensions:
- ESLint - Catches unescaped entities in real-time
- Prettier - Formats code consistently
- Auto Rename Tag - Helps prevent malformed JSX
Auto-fix on Save:
Our .vscode/settings.json
automatically fixes these issues when you save.
Prevention Tools
- ESLint Rule:
react/no-unescaped-entities
(already configured) - Pre-commit Hook: Prevents commits with unescaped entities
- Lint-staged: Runs ESLint on staged files before commit
Common Scenarios
Legal/Privacy Text
// Common in legal documents
<p>We ("Company") don't share your data</p>
User Messages
// Empty states and notifications
<p>You don't have any active subscriptions</p>
<p>Settings couldn't be saved</p>
Technical Documentation
// API docs or technical content
<code>Use } to close objects</code>
<p>Response time > 200ms indicates issues</p>
Testing Your Changes
Run these commands to check for issues:
# Check for ESLint errors
npm run lint
# Fix automatically where possible
npm run lint -- --fix
# Check specific file
npx eslint src/pages/YourFile.tsx
Questions?
If you’re unsure about escaping, check:
- Run ESLint - it will catch most issues
- Look at existing files for examples
- Use template literals
{
content}
for complex text - Ask in code review if you’re uncertain
Remember: Better to over-escape than under-escape!