Exploring the world of animation in React can be an exciting journey, especially

In my useState, I have an array of objects

const [tasks,setTasks] = useState([{completed: true},{completed: false}])

I decided to sort the tasks so that completed ones are at the bottom.

<div>
  {tasks.map((elem) => (
    <div
      onClick={
        //I want to change completed to true if it's currently false. Any ideas on how to create a CSS animation that makes it look like the task is falling down?
      }
    >
      {elem.name}
    </div>
  ))}
</div>;

I attempted using shuffle and ReactCSSTransition, but they are outdated and not effective. Are there any other libraries I can use for this purpose?

Answer №1

Include a unique identifier in the state.

const [tasks, setTasks] = useState([
 { id: 1, completed: true, name: "Task 1" },
 { id: 2, completed: false, name: "Task 2" },
]);

Full code:

const [tasks, setTasks] = useState([
{ id: 1, completed: true, name: "Task 1" },
{ id: 2, completed: false, name: "Task 2" },
]);
const [updatedTasks, setUpdatedTasks] = useState([]);

useEffect(() => {
 if (updatedTasks.length > 0) {
   setTasks(updatedTasks);
   setUpdatedTasks([]);
 }
}, [updatedTasks]);

return (
 <div>
    {tasks.map((elem) => (
     <div
       onClick={() => {
         const updatedTask = tasks.map((task) =>
           task.id === elem.id
            ? { ...task, completed: !task.completed }
            : task
        );
        setUpdatedTasks(updatedTask);
      }}
    >
      <div className={elem.completed ? "" : "fall-down"}>{elem.name}</div>
    </div>
  ))}
</div>

add css :

.fall-down {
   animation: fallDown 0.5s ease-out forwards;
 }

@keyframes fallDown {
  0% {
  transform: translateY(0);
  opacity: 1;
}
100% {
  transform: translateY(50px);
  opacity: 0;
 }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for changing an image with another image upon button click in Angular 6

I have a button with an image and text. When the button is clicked, I want to change both the image and content. Here's the code I'm currently using: <div class="btn-default"> <button name="Save" [ngClass]="[bntStyle]" (click)="submit ...

Issue with triggering onFinish in antd Form component

My onFinish function is responsible for making modifications to the values/json being sent to the backend API. Here is a snippet of how it's implemented: const onFinish = formProps.onFinish!; formProps.onFinish = (values: any) => { ...

What steps should I follow to convert the cookie values into a 'key: value' array?

I need to create a comprehensive list of cookies along with their values, retrieve the values, and then store them in a different location var cookieNames = ["Cookie1", "Cookie2", "Cookie3"]; function getCookie(cookieName) { let name = cookieName + "="; ...

The blending of Angular 4 HTTP responses creates a jumbled mess

I have encountered an issue while using Angular 4 across different browsers like safari, chrome, and Firefox. In my Angular 4 application, I am sending XHR Requests via httpclient to interact with a REST service. The communication is done in JSON format. ...

Is it necessary to re-initialize the store with different preloaded states when navigating between pages in NextJS using Redux?

Trying to integrate Redux with NextJS has been a bit challenging for me. In the official NextJS repository, there is an example that demonstrates how to do this: https://github.com/vercel/next.js/tree/canary/examples/with-redux-thunk The key part of the ...

What is the reason for font names appearing yellow rather than blue in the "font-family" property?

Whenever I experiment with different font names, I notice that some fonts turn blue while others remain yellow. Can anyone clarify why this is happening? font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; ...

Unable to execute the "Writing Assertions" Playwright example due to technical difficulties

TL;DR Having trouble with the "Writing Assertions" example from the Playwright Getting Started. Full Story Exploring Playwright for test writing and going through the Getting Started guide. Encountering issues with Example #2 (Writing Assertions). Curre ...

Incorporate the NestJS module seamlessly into one module from another module without the need to import it

As a newcomer to nestJs, I am currently in the process of creating a module named example.module. Within this module, I am importing another module called DB.Module. However, I have encountered an error when I do not import DB.Module in App.Module. My conc ...

Guide to dynamically generating Angular watchers within a loop

I'm looking to dynamically create angular watches on one or more model attributes. I attempted the following approach: var fields = ['foo', 'bar']; for (var i=0, n=fields.length; i<n; i++) { $scope.$watch('vm.model.&ap ...

Unique design elements of chevron and circle on the Slick Slider vanish upon clicking

I have successfully customized the left and right chevron buttons with a circular design for the Slick Slider Carousel. However, I am facing an issue where clicking on either the left or right chevron causes the circle to disappear. The circle reappears wh ...

Text Alignment Issue in Icon Bar of Zurb Foundation 5

There's an unusual problem I'm encountering with the alignment of text under the icon bar not being properly centered below the icons. Here is a snippet of the code: <div class="row"> <div class="small-12 columns"> < ...

Toggle the visibility of each item in the list using jQuery

I am trying to update the innerHTML property of a div element for each name in a list and animate it using fadeIn, delay for a specific time, and fadeOut. <div id="welcomeBox">Welcome SOMETHING</div> var list = ["George","Bob","Tom"]; $.each ...

The npm audit tool uncovers unexpected dependencies in your project

When running npm audit, I receive warnings along with strange dependencies in the form of random hexadecimal strings. These strings change every time I run npm audit and are the same for all packages mentioned in the audit report. How can I remove these o ...

"Using Javascript to trigger a postback on the parent page from the child page

When I click a button, I am invoking a JavaScript function from the child ASPX page. function closeChildWindow() { window.opener.document.forms(0).submit(); self.close(); } This code snippet closes the child page and tri ...

There appears to be an issue with the onmousemove function

I am currently troubleshooting an issue with a script that I wrote for my button. Interestingly, the code works flawlessly when I test it on CodePen, but I encountered an error when I attempted to run it in VSCode. Even after trying to recreate the script ...

Turning JavaScript Object into a Decimal Value

I've been working on an application where I want to be able to add up the columns of an HTML table. I managed to add inputs to the table successfully, but when trying to sum up the columns, I keep getting a "NaN" error. /* Function for calculating ...

Trouble with the page not updating after creating or updating a task

Currently, I am developing a project using Next.js along with Drizzle ORM. The main functionality involves creating and updating tasks. However, after submitting the task form, the page navigates back to the homepage, but the newly created/updated task doe ...

Unusual glitch involving padding

Recently, I created a basic search application using React and incorporating Bootstrap's Grid system. However, I encountered an issue where the entire interface shifts to the left by approximately 10px when four or more products are rendered. https:/ ...

Presentation for a div's background image with the use of jQuery

My header contains a large div with text contents and boxes, along with a background image. Now I want to create a slideshow for this div's background. Does anyone know how to make a slideshow for a div's background image? I've searched ex ...

React Syntax Error: Regular Expression Unfinished

Within my React project, I am assigning the news variable to the setNews state. To utilize the map method in my JSX, I am passing news as a destructured prop. However, when attempting to nest specific data fields from the API that I am using, an error aris ...