【发布时间】:2018-02-28 09:54:19
【问题描述】:
我需要在我的 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 实践吗?还有其他“好”的方法吗?
【问题讨论】:
-
setState(prevState)由 React 提供,props都是可选的。
标签: javascript reactjs