You’re tasked with updating a greeting message on a webpage. In the vanilla JavaScript example below, the message is set directly:
document.getElementById('app').innerHTML = '<h1>Hello, world!</h1>';
Now, imagine the client wants the message to change to "Hola Mundo" when a button is clicked. Why might React be a better choice than vanilla JavaScript for this task?
React forces you to write more code, which ensures better organization.
Vanilla JavaScript requires you to manually rewrite the entire DOM structure each time.
React uses a virtual DOM to efficiently update only the changed parts of the UI.
React lets you define reusable components, making it easier to manage multiple greetings.
Vanilla JavaScript cannot handle button clicks without a library.
Look at this React code from the lecture:
function Hello() { return <h1>Hello, world!</h1>; } ReactDOM.render(<Hello />, document.getElementById('root'));
You follow the exercise and change "Hello, world!" to "Bonjour le monde!" (French for "Hello World"). After clicking "Play," the new message appears on the screen. What does this demonstrate about React?
"Hello, world!"
"Bonjour le monde!"
React requires a full page reload to show the new message.
React automatically updates the UI when component output changes.
ReactDOM.render() must be called again manually to see the update.
The virtual DOM compares the old and new output to minimize DOM changes.
A small e-commerce site wants to display a reusable "ProductCard" component for items like "Laptop" and "Phone." Here’s a React attempt:
function ProductCard() { return <div>Laptop</div>; } render(<ProductCard />, document.getElementById('root'));
What’s a limitation of this code, and how could React’s features address it?
React components can accept props to dynamically display different products.
The code won’t work because ReactDOM.render() can’t handle custom components.
It’s less efficient than vanilla JS because React always rewrites the entire DOM.
It’s hardcoded to "Laptop" and can’t reuse the component for "Phone" without props.
Imagine a scenario: A team builds a webpage with vanilla JavaScript, manually updating the DOM for every user interaction (e.g., adding items to a cart). The site slows down as interactions increase. How does React’s approach differ?
React uses an imperative approach, giving developers full control over DOM updates.
React’s virtual DOM calculates the smallest set of changes needed, improving performance.
React avoids direct DOM manipulation by managing a virtual representation of the UI.
React eliminates the need for JavaScript, relying solely on HTML.
In the lecture, React is described as having a vast ecosystem (e.g., Next.js, React Router). Suppose you’re building a multi-page app with a homepage, about page, and contact page. Why might this ecosystem matter?
It allows you to avoid writing any custom JavaScript code.
It provides pre-built tools like React Router to handle page navigation declaratively.
It offers reusable components and libraries to simplify complex features like routing.
It guarantees the app will run faster than vanilla JS without extra effort.
This content is only available to registered users.Please register at the the home page.