Bird
Raised Fist0
Reactframework~10 mins

Why React is used - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a React component that displays a greeting.

React
function Greeting() {
  return <h1>{ [1] }</h1>;
}
Drag options to blanks, or click blank then click option'
A"Hello, world!"
BHello, world!
C<Hello, world!>
D{Hello, world!}
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around text inside JSX.
Using curly braces without quotes for plain text.
2fill in blank
medium

Complete the code to use React's useState hook to create a counter.

React
import React, { [1] } from 'react';
Drag options to blanks, or click blank then click option'
AuseRef
BuseEffect
CuseContext
DuseState
Attempts:
3 left
💡 Hint
Common Mistakes
Importing useEffect instead of useState.
Trying to use state without importing useState.
3fill in blank
hard

Fix the error in the React component that updates state on button click.

React
function Counter() {
  const [count, setCount] = React.useState(0);
  return (
    <button onClick=[1]>
      Count: {count}
    </button>
  );
}
Drag options to blanks, or click blank then click option'
A() => setCount(count + 1)
Bcount + 1
C() => count + 1
DsetCount(count + 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling setCount directly instead of passing a function.
Using expressions instead of functions for onClick.
4fill in blank
hard

Fill both blanks to create a controlled input component in React.

React
function NameInput() {
  const [name, setName] = React.useState('');
  return (
    <input
      value=[1]
      onChange={e => [2](e.target.value)}
      aria-label="Name input"
    />
  );
}
Drag options to blanks, or click blank then click option'
Aname
BsetName
Cname()
DsetName()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name()' instead of 'name' for value.
Calling 'setName()' without argument.
5fill in blank
hard

Fill all three blanks to create a React component that fetches data on mount using useEffect.

React
import React, { [1], [2] } from 'react';

function DataFetcher() {
  const [data, setData] = [3](null);

  [1](() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(json => setData(json));
  }, []);

  if (!data) return <p>Loading...</p>;
  return <pre>{JSON.stringify(data, null, 2)}</pre>;
}
Drag options to blanks, or click blank then click option'
AuseEffect
BuseState
DuseContext
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to import useEffect or useState.
Using useContext instead of useState for state.

Practice

(1/5)
1. Why do developers use React for building websites?
easy
A. To design database schemas
B. To write backend server code easily
C. To manage network security settings
D. To create reusable components that make development faster

Solution

  1. Step 1: Understand React's main feature

    React breaks websites into small, reusable parts called components, which helps developers build faster and maintainable code.
  2. Step 2: Compare options with React's purpose

    Only To create reusable components that make development faster describes creating reusable components, which is React's core use. Other options relate to backend, database, or security, which React does not handle.
  3. Final Answer:

    To create reusable components that make development faster -> Option D
  4. Quick Check:

    Reusable components = C [OK]
Hint: React is mainly for building UI components fast [OK]
Common Mistakes:
  • Confusing React with backend frameworks
  • Thinking React manages databases
  • Assuming React handles security
2. Which of the following is the correct way to define a React functional component?
easy
A. component MyComponent() { return
Hello
; }
B. function MyComponent() { return
Hello
; }
C. def MyComponent() { return
Hello
; }
D. func MyComponent() { return
Hello
; }

Solution

  1. Step 1: Recall React functional component syntax

    React functional components are JavaScript functions that return JSX, defined using the keyword 'function'.
  2. Step 2: Check each option's syntax

    function MyComponent() { return
    Hello; } uses 'function' correctly. Options B, C, and D use invalid keywords not recognized in JavaScript or React.
  3. Final Answer:

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

    React components use 'function' keyword [OK]
Hint: React components start with 'function' keyword [OK]
Common Mistakes:
  • Using Python or other language keywords
  • Confusing component declaration syntax
  • Omitting return statement or JSX
3. What will be the result of this React component rendering?
function Greeting() {
  const name = 'Alice';
  return <h1>Hello, {name}!</h1>;
}
medium
A. Syntax Error
B. Hello, {name}!
C. <h1>Hello, Alice!</h1>
D. <h1>Hello, name!</h1>

Solution

  1. Step 1: Understand JSX expression embedding

    In JSX, curly braces { } embed JavaScript expressions. Here, {name} inserts the value of the variable 'name', which is 'Alice'.
  2. Step 2: Predict rendered output

    The component returns an h1 element with text 'Hello, Alice!'. So the rendered HTML is <h1>Hello, Alice!</h1>.
  3. Final Answer:

    <h1>Hello, Alice!</h1> -> Option C
  4. Quick Check:

    JSX {name} inserts variable value [OK]
Hint: JSX uses { } to insert variables [OK]
Common Mistakes:
  • Thinking {name} prints literally
  • Confusing JSX with plain HTML
  • Expecting syntax error for valid JSX
4. Identify the error in this React component code:
function Welcome() {
  return (
    <div>
      <h1>Welcome</h1>
      <p>Have a nice day!</p>
    </div>
  )
  console.log('Rendered');
}
medium
A. console.log is unreachable after return statement
B. Missing semicolon after return
C. JSX tags are not closed properly
D. Function name should be lowercase

Solution

  1. Step 1: Analyze function flow

    The return statement ends the function execution, so any code after it is unreachable.
  2. Step 2: Check the console.log placement

    console.log('Rendered') is after return, so it will never run, which is a logical error.
  3. Final Answer:

    console.log is unreachable after return statement -> Option A
  4. Quick Check:

    Code after return is unreachable [OK]
Hint: Code after return won't run [OK]
Common Mistakes:
  • Ignoring unreachable code warnings
  • Thinking semicolon is mandatory after return
  • Assuming function names must be lowercase
5. How does React improve website speed when updating the user interface?
hard
A. By updating only the parts of the page that change using a virtual DOM
B. By reloading the entire page on every change
C. By using server-side rendering exclusively
D. By disabling user interactions during updates

Solution

  1. Step 1: Understand React's update mechanism

    React uses a virtual DOM to track changes and updates only the parts of the real DOM that need to change, avoiding full page reloads.
  2. Step 2: Compare options with React's behavior

    By updating only the parts of the page that change using a virtual DOM correctly describes this selective update process. Options B, C, and D describe incorrect or unrelated behaviors.
  3. Final Answer:

    By updating only the parts of the page that change using a virtual DOM -> Option A
  4. Quick Check:

    Virtual DOM updates only changed parts [OK]
Hint: React updates only changed parts for speed [OK]
Common Mistakes:
  • Thinking React reloads whole page
  • Confusing client and server rendering
  • Assuming React disables interactions
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