最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - React: How could I add a fade out animation on delete on dynamically generated table entries? - Stack Overflow

matteradmin7PV0评论

For several days I have been trying to figure out how to add a fade out animation on dynamically generated table in React, but I had no luck in it yet.

Seems like every approach I try has some limitations to it and does not work in my case: 1) I tried to set it up with refs, but it seems that I cannot dynamically add and correctly store those refs in my React ponent for a table that has multiple dynamically generated entries. 2) ReactTransitionGroup has its limitations in sense that when I wrap my parent element in TransitionGroup my table design seems to shift away and move everything under the first column. As well as the fade out functionality has not worked for me.

What would be your remend approach for such task? Is there any library that I could try for this sort of problem?

Best regards, Konstantin

P.S. Here is the link on my source code.

For several days I have been trying to figure out how to add a fade out animation on dynamically generated table in React, but I had no luck in it yet.

Seems like every approach I try has some limitations to it and does not work in my case: 1) I tried to set it up with refs, but it seems that I cannot dynamically add and correctly store those refs in my React ponent for a table that has multiple dynamically generated entries. 2) ReactTransitionGroup has its limitations in sense that when I wrap my parent element in TransitionGroup my table design seems to shift away and move everything under the first column. As well as the fade out functionality has not worked for me.

What would be your remend approach for such task? Is there any library that I could try for this sort of problem?

Best regards, Konstantin

P.S. Here is the link on my source code.

Share Improve this question asked Jun 15, 2020 at 11:44 Konstantink1Konstantink1 6352 gold badges15 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

There is another react library react-springs which can help you with that.

Easier alternative would be to place style and onTransitionEnd props in tr element and store opacity of rows in state.

const { useState, useEffect } = React;

const App = () => {
  const [rows, setRows] = useState(Array(3).fill(0).map((_, index) => ({
    id: index,
    opacity: 1
  })));

  const onClick = (id) => {
    setRows(rows => {
      const newRows = [...rows];
      const row = newRows.find(row => row.id === id);
      row.opacity = 0;
      
      return newRows
    });
  }
  
  const onTransitionEnd = (id) => {
    console.log(id);
    setRows(rows => rows.filter(pr => pr.id !== id));
  }

  return <div>
      <table>
        <tbody>
          {rows.map(({id, ...styles}) => <tr onTransitionEnd={() => onTransitionEnd(id)} style={styles} key={id}>
            <td>{id + 1}</td>
            <td>{id + 2}</td>
            <td>{id + 3}</td>
            <td className="delete" onClick={() => onClick(id)}><div >Delete</div></td>
          </tr>)}
        </tbody>
      </table>
    </div>
}

ReactDOM.render(
    <App />,
    document.getElementById('root')
  );
table {
  border-collapse: collapse;
}

tr {
  transition: all .5s ease-in;
}

td {
  padding: 10px;
  border: 1px solid lightgray;
}

.delete {
  cursor: pointer;
}
<script src="https://unpkg./react/umd/react.development.js"></script>
<script src="https://unpkg./react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg./babel-standalone@6/babel.min.js"></script>
<div id="root"></div>

If you are still looking for an answer you can try modifying "handleDeletion" function similar to below snippet.

function handleDeletion(event) {
    const id = event.currentTarget.value
    const container = event.currentTarget.closest("tr");
    container.style.transition = "all 0.5s";
    container.style.opacity = "0";
    setTimeout(function() {
        setDataset(prevDataset => prevDataset.filter(item => item.id !== parseInt(id)))
        localStorage.removeItem(id)
    }, 700)
}

In this solution table borders wont go away gently. It may look better with css grid or flex.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far