DEV Community

Cover image for 20 Essential React Tricks Every Developer Must Know

20 Essential React Tricks Every Developer Must Know

Jagroop Singh on October 31, 2024

React is incredibly powerful, but mastering it means going beyond the basics and learning some lesser-known tricks to streamline development. Here’...
Collapse
 
paxnw profile image
caga

Yes this one :
Export Components with Named Exports for Better Import Control
My senior also pointed out me to use like this as explained in 19 tip instead of export default.
What's the reason behind using this and why not I prefer export deafault componentA.

Collapse
 
jagroop2001 profile image
Jagroop Singh

@paxnw ,
Using named exports instead of default exports provides better control and clarity when importing components.
With named exports, you can import multiple components from a single module, which helps avoid naming collisions and makes it clear what is being imported.
This practice also enhances code readability and maintainability by explicitly defining what components are available for use, making it easier to track dependencies in larger codebases.

Collapse
 
dasheck0 profile image
Stefan Neidig

In 12 what is the benefit of prop types over using Interfaces. Haven't seen this before so I was wondering.

Collapse
 
jagroop2001 profile image
Jagroop Singh

@dasheck0 ,
In React (if I talk about JS based), PropTypes allow runtime type checking for props, catching errors during execution, whereas TypeScript interfaces provide static type checking at compile time, which can prevent errors earlier.

Collapse
 
dasheck0 profile image
Stefan Neidig

I see. Thanks for clarifying

Collapse
 
joels profile image
Joel Sullivan

Personally I prefer TypeScript interfaces over the older PropTypes.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Yes me too, but for javascript based React project it's very handy. @joels

Collapse
 
john12 profile image
john

Destructuring props directly in the function parameters is my favorite approachβ€”it enhances code readability significantly. While some people argue that it's too common to be noteworthy, I disagree. Previously, I worked on a React project with code that was messy: excessive prop drilling and constant references like props.name and props.user.address.pincode. Dealing with this was frustrating, which is why destructuring has become my go-to.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Exactly @john12 !!

Collapse
 
anjantalatam profile image
Anjan Talatam

Prefer clsx over classnames. classnames is ~3 times bigger than clsx.
npm trends

Image description

Collapse
 
bare-metal-email profile image
Bare Metal Email

Nice

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks

Collapse
 
darksmile92 profile image
Robin Kretzschmar

I prefer classcat over classnames package for cases where I don't want to use interpolated strings. I saw some performance comparisons some time ago and stuck with it ever since.

Collapse
 
festus891 profile image
Aderibigbe Festus Adewale

hbb