DEV Community

Cover image for Essential React js Shorthand Techniques
Vishnu Satheesh
Vishnu Satheesh

Posted on

Essential React js Shorthand Techniques

In the realm of modern web development, React.js stands tall as one of the most popular and powerful JavaScript libraries for building user interfaces.
As developers, we are constantly seeking ways to streamline our code, making it more efficient, readable, and maintainable.
In this blog, we delve into the world of React shorthand techniques - a collection of concise and elegant methods to achieve common tasks with less code.

Conditional Rendering with Logical &&

Render components conditionally using the && operator for cleaner, more readable code.

Bad code:


{
  isLoggedIn ? <LogoutButton /> : null
}


Enter fullscreen mode Exit fullscreen mode
Good code:


{
  isLoggedIn && <LogoutButton />
}


Enter fullscreen mode Exit fullscreen mode

Destructuring Props and State

Use object destructuring to extract values from props and state for more concise and readable component code.

Bad code:


const value = props.value


Enter fullscreen mode Exit fullscreen mode
Good code:


const { value } = props


Enter fullscreen mode Exit fullscreen mode

Fragment Short Syntax

Utilize the short syntax <>...</> to group a list of children without adding extra nodes to the DOM.

Bad code:


<React.Fragment>
  <ComponentA />
  <ComponentB />
</React.Fragment>



Enter fullscreen mode Exit fullscreen mode
Good code:


<>
  <ComponentA />
  <ComponentB />
</>


Enter fullscreen mode Exit fullscreen mode

Arrow Function in Event Handlers

Use arrow functions directly in JSX for event handling to avoid binding this in the constructor.

Bad code:


<button onClick={
  this.handleClick.bind(this)
}> Click </button>


Enter fullscreen mode Exit fullscreen mode
Good code:


<button onClick={
  () => this.handleClick()
}> Click </button>


Enter fullscreen mode Exit fullscreen mode

Function Component Declaration

Define functional components using arrow function for a more concise syntax

Bad code:


function Welcome(props){
  return <h1>Hello, {props.name}</h1>
}


Enter fullscreen mode Exit fullscreen mode
Good code :


const Welcome = ({ name }) =>
  <h1>Hello, {name}</h1>


Enter fullscreen mode Exit fullscreen mode

Optional Chaining for Property Access

Use optional chaining (?.) to safely access nested object properties without checking each level

Bad code:


  const name = props.user && props.user.name


Enter fullscreen mode Exit fullscreen mode
Good code:


  const name = props.user?.name


Enter fullscreen mode Exit fullscreen mode

Spread Attributes

Spread attributes to pass all properties of an object to props as a component, simplifying the the passing of props.

Bad code:


<MyComponent
  prop1={props.prop1}
  prop2={props.prop2}
/>


Enter fullscreen mode Exit fullscreen mode
Good code :


<MyComponent {...props} />


Enter fullscreen mode Exit fullscreen mode

Nullish Coalescing Operator for Default Props

Using the nullish coalescing operator (??) to provide default values for props.

Bad code:


const username = props.username ? props.username : 'Guest';


Enter fullscreen mode Exit fullscreen mode
Good code:


const username = props.username ?? 'Guest';


Enter fullscreen mode Exit fullscreen mode

Don't let bloated code weigh down your react projects. Embrace shorthand techniques to make your code more concise, efficient, and readable. These techniques will not only make your development process smoother but will also elevate the quality of your applications. Dive into shorthand techniques today and transform your React journey!

Stay tuned for more insights and practical tipsπŸš€

Buy Me A Coffee

Top comments (2)

Collapse
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop

Nice examples 😌.
Some suggestions:

  • Only use the first trick when isLoggedIn is actually a Boolean to prevent invalid values on the UI.
  • For React.Fragment we still need to use it if we want to pass a key.
Collapse
 
vishnusatheesh profile image
Vishnu Satheesh

Thank you so much for the suggestions 😊