当存储在状态中的数据值发生变化时,我试图在 HTML 元素上制作一个小动画。如何实现?
如何在 componentWillUpdate
中将 CSS 类添加到组件并在 componentDidUpdate
中将其删除?我在那里没有看到对任何 HTML 元素的任何引用。
最佳答案
如果需要给组件添加类:React.findDOMNode(this).classList.add("classname");
删除:
React.findDOMNode(this).classList.remove("classname");
如果您尝试在 componentWillUpdate
中添加一个类并在 componentDidUpdate
中删除它,您需要使用类似 setTimeout 的方法来通知更改。例如:
componentWillUpdate: function() {
React.findDOMNode(this).classList.add("class1", "class2");
},
componentDidUpdate: function() {
var el = React.findDOMNode(this);
setTimeout(function(){
el.classList.remove("class1");
}, 1000);
}
关于javascript - React.js 中状态变化时动画组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30764693/