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

For several days I have been trying to figure out how to add a fade out animation on dynamically genera

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745660637a4638813.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信