admin管理员组文章数量:1026989
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 badges2 Answers
Reset to default 2There 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.
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 badges2 Answers
Reset to default 2There 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.
本文标签:
版权声明:本文标题:javascript - React: How could I add a fade out animation on delete on dynamically generated table entries? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660636a2161867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论