React

Best practices for React JS developer

Best practices for React JS developer

Today we are going to see best practices for React JS developer

  1. Passing a boolean variables.
  2. Do not define a function within a render
  3. Naming of the components
  4. Use of the Ternary Operators
  5. Don’t Need Curly Braces in string

Passing a boolean variables

Passing boolean variables in to two components.

Not Good

 

				
					return (
   <Home showTitle={true}/>
); 
				
			

Good

				
					return (
   <Home showTitle/>
); 
				
			

Do not define a function within a render

Don’t define a function inside render.

Not Good

 

				
					return (
    <button onClick={() => dispatch(ACTION_TO_SEND_DATA)}>
      Example
    </button>  
)
				
			

Good

				
					const submitData = () => dispatch(ACTION_TO_SEND_DATA)
return (
  <button onClick={submitData}>  
    Example 
  </button>  
)
				
			

Naming of the components

You can use PascalCase for name of the components and camelCase for name of the instances.

Not Good

 

				
					import createCard from './CreateCard';
const ReservationItem = <ReservationCard />;
				
			

Good

				
					import CreateCard from './CreateCard';
const reservationItem = <ReservationCard />;
				
			

Use of the Ternary Operators

Developer how can use ternary operator in React js.

Not Good

				
					const { role } = user;
if(role === 'ADMIN') {
  return <AdminUser />
}else{
  return <NormalUser />
} 
				
			

Good

				
					const { role } = user;
return role === 'ADMIN' ? <AdminUser /> : <NormalUser />
				
			

Don’t Need Curly Braces in string

When developer passing string props to a children component

Not Good

				
					return(
  <Navbar title={"My Special App"} />
)
				
			

Good

 

				
					return(
<Navbar title="My Special App" />  
)
				
			

Hope this helps for React js developer

 

Share On: