Sobes.tech
Middle

There are three anti-patterns in the handleCityChange function on line 52. Find and explain them.

sobes.tech AI

Answer from AI

It is difficult to specify without a specific code, but typical anti-patterns in the handleCityChange function on line 52 may include:

  1. Mutating state directly — changing the component's or global state directly without using setState or appropriate methods, which violates immutability principles and can lead to bugs.

  2. A large function with multiple responsibilities — if handleCityChange performs several tasks at once (e.g., updating state, making requests, triggering side effects), it complicates maintenance and testing.

  3. Lack of error handling or asynchronous operations — if the function calls asynchronous operations (e.g., API requests) without error handling or without using async/await, it can lead to unpredictable behavior.

An example of bad practice:

function handleCityChange(event) {
  city = event.target.value; // direct variable change
  fetch('/api/data?city=' + city) // without error handling
    .then(response => response.json())
    .then(data => {
      // direct state update
      state.data = data;
    });
}

It is better to use setState, separate logic, and handle errors.

There are three anti-patterns in the handleCityChange… - sobes.tech