Kyle Shevlin 8/20/2024

Never Call <code>new Date()</code> Inside Your Components

Read Original

The article argues against using impure functions like new Date() inside React components, especially for setting initial state, as it creates unpredictable behavior and flaky visual regression tests. It demonstrates the problem with a date input example and provides a solution: passing the date as a prop (or using default parameters) to make components pure and easily testable with static values.

Never Call <code>new Date()</code> Inside Your Components

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week