我需要在我的 React 组件中使用当前的 props 和以前的 props 值。所以我是这样做的
state = {
current: null,
previous: null,
};
componentWillReceiveProps(nextProps) {
if (nextProps.amount !== this.state.current) {
this.setState({previous: this.state.current, current: nextProps.amount});
}
}
...
render() {
const {previous, current} = this.state;
return (
...
<CountUp className="counter" start={previous} end={current} duration={1}/>
...
)
}
它工作正常,但这样做是否是一种好的 React 实践?还有其他“好”的方法可以做到吗?