InterviewHubPRO
Section Overview
Q65

Question 65 of 445

Part 1: Core React, JSX & Component Architecture (1 - 77)

15%
React & Modern Frontend (445 Qs)•Part 1: Core React, JSX & Component Architecture (1 - 77)

How to pretty print JSON with React?

We can use <pre> tag so that the formatting of the JSON.stringify() is retained:

jsx
const data = { name: "John", age: 42 };

function User {
 return <pre>{JSON.stringify(data, null, 2)}</pre>;
}

const container = createRoot(document.getElementById("container"));

container.render(<User />);
<details><summary><b>See Class</b></summary> <p>
jsx
const data = { name: "John", age: 42 };

class User extends React.Component {
render() {
 return <pre>{JSON.stringify(data, null, 2)}</pre>;
}
}

React.render(<User />, document.getElementById("container"));
</p> </details>