Build React's renderToStaticMarkup: escaping, attribute and style rules, void elements, components and context
Problem
renderToStaticMarkup turns a React element tree into an HTML string with nothing extra for hydration: email templates, static pages, RSS, the HTML of a PDF. Writing it yourself forces you to know what a React element really is, how components are called, and the small rules React applies to every prop. Those rules are also where XSS bugs live. Build it, and match React's output exactly: the tests compare your string with react-dom/server.
renderToStaticMarkup(<label className="field" htmlFor="email" onClick={save} style={{ fontSize: 12 }}>Email</label>);
// '<label class="field" for="email" style="font-size:12px">Email</label>'Ref. UR-1440 · © UIRound · uiround.com/questions/build-reacts-rendertostaticmarkup-escaping-attribute-and-style-rules-void-elements-components-and-context
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 6
- edge cases
- 4
- follow-up questions
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1A React element is a plain object with
typeandprops. Write one recursive function over nodes: what does it do for a string, an array,null, a host element (typeis a string) and a component?
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.