当存储在状态中的数据值改变时,我想在HTML元素上制作一个小动画。如何实现?
如何在componentWillUpdate
中向组件添加CSS类并在componentDidUpdate
中删除它?我没有看到任何参考任何HTML元素的内容。
当存储在状态中的数据值改变时,我想在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);
}
componentWillUnmount
中清除那个超时。 - IliasT