Bird
Raised Fist0
Reactframework~5 mins

React vs traditional JavaScript

Choose your learning style10 modes available
Introduction

React helps build user interfaces in a clear, organized way. Traditional JavaScript changes the page directly, which can get messy.

When you want to build interactive web pages that update smoothly.
When your page has many parts that change often, like a chat or a to-do list.
When you want to write code that is easier to read and maintain.
When you want to reuse parts of your page in different places.
When you want to handle user actions like clicks or typing in a simple way.
Syntax
React
React example:
function Hello() {
  return <h1>Hello, world!</h1>;
}

Traditional JavaScript example:
document.body.innerHTML = '<h1>Hello, world!</h1>';

React uses components that return HTML-like code called JSX.

Traditional JavaScript changes the page by directly modifying HTML elements.

Examples
This React component shows a welcome message using JSX.
React
function Greeting() {
  return <p>Welcome to React!</p>;
}
This traditional JavaScript code changes the content inside an element with id 'root'.
React
document.getElementById('root').innerHTML = '<p>Welcome to JavaScript!</p>';
Sample Program

This React component shows a button and a number. Each time you click the button, the number goes up. React updates the page automatically.

React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

export default Counter;
OutputSuccess
Important Notes

React keeps the page in sync with your data automatically.

Traditional JavaScript requires you to manually update the page when data changes.

React components make it easier to organize and reuse code.

Summary

React uses components and JSX to build pages clearly.

Traditional JavaScript changes the page directly and can be harder to manage.

React helps with interactive and dynamic user interfaces.

Practice

(1/5)
1. What is a key difference between React and traditional JavaScript when building web pages?
easy
A. React requires writing HTML only, traditional JavaScript uses CSS only.
B. React uses components and JSX, while traditional JavaScript manipulates the DOM directly.
C. React cannot update the page dynamically, traditional JavaScript can.
D. React uses server-side rendering only, traditional JavaScript is client-side only.

Solution

  1. Step 1: Understand React's approach

    React builds UI using components and JSX, which is a syntax that looks like HTML inside JavaScript.
  2. Step 2: Understand traditional JavaScript approach

    Traditional JavaScript changes the page by directly manipulating the DOM elements, which can be more complex and error-prone.
  3. Final Answer:

    React uses components and JSX, while traditional JavaScript manipulates the DOM directly. -> Option B
  4. Quick Check:

    React uses components and JSX = A [OK]
Hint: React uses components; traditional JS changes DOM directly [OK]
Common Mistakes:
  • Thinking React only uses HTML or CSS
  • Believing React cannot update pages dynamically
  • Confusing server-side and client-side rendering
2. Which of the following is the correct way to create a React functional component?
easy
A. class MyComponent extends React.Component { render() { return 'Hello'; } }
B. var MyComponent =
Hello
;
C. function MyComponent() { return
Hello
; }
D. function MyComponent() { document.write('Hello'); }

Solution

  1. Step 1: Identify React functional component syntax

    A React functional component is a function returning JSX, like function MyComponent() { return <div>Hello</div>; }.
  2. Step 2: Check other options for correctness

    var MyComponent =
    Hello; assigns JSX directly to a variable, not a function. class MyComponent extends React.Component { render() { return 'Hello'; } } is a class component, not functional. function MyComponent() { document.write('Hello'); } uses document.write, which is not React.
  3. Final Answer:

    function MyComponent() { return <div>Hello</div>; } -> Option C
  4. Quick Check:

    Functional component returns JSX function = A [OK]
Hint: Functional components are functions returning JSX [OK]
Common Mistakes:
  • Confusing class components with functional components
  • Using document.write inside React components
  • Assigning JSX to variables without functions
3. What will be the output on the page when this React component is rendered?
function Greeting() {
  const name = 'Alice';
  return <h1>Hello, {name}!</h1>;
}
medium
A. Hello, Alice!
B. Hello, name!
C. <h1>Hello, {name}!</h1>
D. Error: name is not defined

Solution

  1. Step 1: Understand JSX expression evaluation

    Inside JSX, curly braces {name} insert the value of the variable name, which is 'Alice'.
  2. Step 2: Determine rendered output

    The component returns <h1>Hello, Alice!</h1>, so the page shows the text 'Hello, Alice!'.
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    JSX inserts variable value = C [OK]
Hint: JSX {variable} shows variable value, not text {variable} [OK]
Common Mistakes:
  • Thinking JSX shows {name} literally
  • Confusing variable name with string output
  • Expecting an error due to variable usage
4. Identify the error in this React component code:
function Counter() {
  let count = 0;
  function increment() {
    count++;
  }
  return <button onClick={increment}>Count: {count}</button>;
}
medium
A. Using let instead of const for count
B. onClick should be written as onclick
C. Missing semicolon after count++
D. The increment function does not update the displayed count

Solution

  1. Step 1: Understand React state behavior

    React components must use state (like useState) to update UI dynamically. Here, count is a normal variable and changes won't re-render the component.
  2. Step 2: Identify why increment doesn't update UI

    Increment changes count variable but React does not know to re-render, so displayed count stays 0.
  3. Final Answer:

    The increment function does not update the displayed count -> Option D
  4. Quick Check:

    State needed to update UI = D [OK]
Hint: Use React state (useState) to update UI, not plain variables [OK]
Common Mistakes:
  • Thinking let vs const affects UI update
  • Ignoring React's re-render on state change
  • Using wrong event attribute case
5. You want to build a dynamic list of items that updates when a user adds a new item. Which approach best uses React instead of traditional JavaScript?
hard
A. Use React's useState hook to store the list and update it, then render the list in JSX.
B. Use document.getElementById to find the list element and append new items manually.
C. Write a for loop to create HTML strings and set innerHTML of the list container.
D. Use React class components with componentDidMount to update the list directly.

Solution

  1. Step 1: Understand React's dynamic UI approach

    React uses state (like useState) to hold data and re-renders UI automatically when state changes.
  2. Step 2: Compare with traditional DOM manipulation

    Traditional JavaScript manipulates DOM directly (options A and C), which is harder to manage and less efficient.
  3. Step 3: Consider React class components

    Use React class components with componentDidMount to update the list directly. uses class components and lifecycle methods, but modern React prefers functional components with hooks.
  4. Final Answer:

    Use React's useState hook to store the list and update it, then render the list in JSX. -> Option A
  5. Quick Check:

    React state and JSX rendering = B [OK]
Hint: Use useState hook for dynamic lists in React [OK]
Common Mistakes:
  • Using direct DOM methods inside React components
  • Relying on innerHTML string building
  • Using outdated class components instead of hooks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard