0
0
Reactframework~20 mins

ReactDOM render process - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ReactDOM Render Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when ReactDOM.render is called twice on the same container?

Consider this React code snippet:

const root = document.getElementById('root');
ReactDOM.render(<h1>First</h1>, root);
ReactDOM.render(<h1>Second</h1>, root);

What will be displayed inside the root element after both calls?

React
const root = document.getElementById('root');
ReactDOM.render(<h1>First</h1>, root);
ReactDOM.render(<h1>Second</h1>, root);
AThe root element will display 'First' only.
BThe root element will display 'Second' only.
CThe root element will display both 'First' and 'Second' headings.
DThe root element will be empty.
Attempts:
2 left
💡 Hint

Think about how ReactDOM.render replaces content in the container.

lifecycle
intermediate
2:00remaining
Which lifecycle behavior occurs when ReactDOM.render updates an existing component?

When ReactDOM.render is called on a container that already has a React component rendered, what lifecycle behavior happens to the existing component?

AThe existing component is updated with new props and re-rendered.
BThe existing component is unmounted and a new one is mounted.
CNothing happens; the component stays the same.
DReact throws an error because the container is not empty.
Attempts:
2 left
💡 Hint

React tries to reuse components when possible.

📝 Syntax
advanced
2:00remaining
What error occurs if ReactDOM.render is called without a valid container?

What happens if you call ReactDOM.render(<App />, null) or with an undefined container?

React
ReactDOM.render(<App />, null);
ANo error; React renders to a default container.
BSyntaxError: Unexpected token '<'
CReferenceError: App is not defined
DTypeError: Cannot read properties of null (reading 'nodeType')
Attempts:
2 left
💡 Hint

ReactDOM.render expects a real DOM element as container.

state_output
advanced
2:00remaining
What is the state value after ReactDOM.render updates a component?

Given this React component:

function Counter() {
  const [count, setCount] = React.useState(0);
  return <div>Count: {count}</div>;
}

If ReactDOM.render(<Counter />, root) is called, then later ReactDOM.render(<Counter />, root) is called again, what will be the displayed count?

React
function Counter() {
  const [count, setCount] = React.useState(0);
  return <div>Count: {count}</div>;
}

ReactDOM.render(<Counter />, root);
// Later
ReactDOM.render(<Counter />, root);
ACount: 0
BCount: 1
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint

Think about whether state persists between separate ReactDOM.render calls.

🔧 Debug
expert
3:00remaining
Why does ReactDOM.render not update the UI after state change in a class component?

Consider this class component:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment() {
    this.state.count += 1;
  }

  render() {
    return <div>Count: {this.state.count}</div>;
  }
}

After calling increment() and then ReactDOM.render(<MyComponent />, root) again, the UI does not update. Why?

React
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment() {
    this.state.count += 1;
  }

  render() {
    return <div>Count: {this.state.count}</div>;
  }
}

// Usage
const root = document.getElementById('root');
const comp = new MyComponent();
comp.increment();
ReactDOM.render(<MyComponent />, root);
ABecause ReactDOM.render cannot render class components.
BBecause increment() was not called inside render method.
CBecause state was mutated directly without using setState, React does not detect the change.
DBecause ReactDOM.render requires a functional component.
Attempts:
2 left
💡 Hint

How does React detect state changes in class components?