W
W
webe2019-01-12 12:41:16
React
webe, 2019-01-12 12:41:16

How to smoothly delete a block?

There is a container, it has 3 blocks that render the this.state.users array,
I click on the button and one of the blocks is removed (disappears from the state)
how to make it disappear smoothly and not instantly?
I understand that there are all sorts of plugins, but I can’t understand how these plugins work under the hood?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2019-01-15
@webe

deletion should take place in 2 stages:
1. mark the element to be deleted with a special flag "isDeleted" (in this state, you start the disappearance animation using css)
2. at the end of the animation, delete the element from the state completely

...
onDeleteItem(index) {
  const users = this.state.users;
  users[index].isDeteted = true;
  this.setState({
    users,
  });
  setTimeout(() => {
    users.splice(index, 1);
    this.setState({
      users,
    });
  }, 300);
}
...
{this.state.users.map((user, index) => (
  <UserItem 
    className={user.isDeteted ? 'class-for-deleted' : ''} 
    onDelete={this.onDeleteItem.bind(this, index)} 
  />
))}
...

senior level: this mechanism has an asynchronous state change, therefore, for a stable code, it is necessary to add a check that the removal process has ended (so that there are no collisions between several quick "delete" clicks), as well as the existence of the current component (that it has not been unmounted from dom-tree)

A
Andrey Okhotnikov, 2019-01-14
@tsepen

By clicking on the block, first remove it using css with some kind of smooth animation, and then immediately remove it from the state

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question