Safe HTML Rendering
ReactJS
Hard
8 views
Problem Description
Render user text safely (do not use dangerouslySetInnerHTML).
Output Format
Render a React component.
Constraints
Show text as plain text and escape automatically.
Official Solution
import React from 'react';
export default function App() {
const userText = '<b>meetcode</b> <script>alert(1)</script>';
return (
<div style={{ padding: 16 }}>
<strong>Message:</strong>
<div style={{ marginTop: 8, border: '1px solid #eee', borderRadius: 14, padding: 12 }}>
{userText}
</div>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!