Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited title
Link
Shubham Khatri
  • 283.9k
  • 58
  • 431
  • 411

What is the best practise to use previous Take an action on props? change in ReactJS

Source Link
Mad Max
  • 283
  • 1
  • 7
  • 20

What is the best practise to use previous props?

I need to use current props and previous props value in my React component. So i did it like this

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}/> ... ) } 

It works fine, but is it good React practise to do it like this? Are there others "good" ways to do it?