0 votes
in ReactJS by
What is the difference between Imperative and Declarative in React?

1 Answer

0 votes
by

Imagine a simple UI component, such as a "Like" button. When you tap it, it turns blue if it was previously grey, and grey if it was previously blue.

The imperative way of doing this would be:

if (user.likes()) {
  if (hasBlue()) {
    removeBlue();
    addGrey();
  } else {
    removeGrey();
    addBlue();
  }
}

Basically, you have to check what is currently on the screen and handle all the changes necessary to redraw it with the current state, including undoing the changes from the previous state. You can imagine how complex this could be in a real-world scenario.

In contrast, the declarative approach would be:

if (this.state.liked) {
  return <blueLike />;
} else {
  return <greyLike />;
}

Because the declarative approach separates concerns, this part of it only needs to handle how the UI should look in a sepecific state, and is therefore much simpler to understand.

Related questions

0 votes
asked Nov 2, 2023 in ReactJS by AdilsonLima
0 votes
asked Nov 10, 2023 in ReactJS by GeorgeBell
...