Logo

dev-resources.site

for different kinds of informations.

Mastering React: Best Practices for Cleaner and More Efficient Code

Published at
1/6/2024
Categories
react
bestpractice
webdev
Author
mehedi
Categories
3 categories in total
react
open
bestpractice
open
webdev
open
Author
6 person written this
mehedi
open
Mastering React: Best Practices for Cleaner and More Efficient Code

āĻāĻ‡ āĻ†āĻ°ā§āĻŸāĻŋāĻ•ā§āĻ¯āĻžāĻ˛ āĻ āĻ†āĻŽāĻ°āĻž React āĻāĻ° āĻ•āĻŋāĻ›ā§ best practice āĻ¨āĻŋā§Ÿā§‡ āĻ•āĻĨāĻž āĻŦāĻ˛āĻŦā§‹ āĨ¤

1. Destructuring and Spread Operator for Props:

React component āĻ āĻāĻ•āĻžāĻ§āĻŋāĻ• prop āĻ°āĻŋāĻ¸āĻŋāĻ­ āĻ•āĻ°āĻ˛ā§‡ āĻ†āĻŽāĻ°āĻž āĻ¸ā§‡āĻ—ā§āĻ˛ā§‹ destructure āĻ•āĻ°ā§‡ āĻ‡āĻ‰āĻœ āĻ•āĻ°āĻ˛ā§‡ āĻ•ā§‹āĻĄ āĻāĻ° readability and efficiency āĻŦā§‡ā§œā§‡ āĻ¯āĻžā§Ÿ āĨ¤

const MyComponent = ({ prop1, prop2, prop3, ...restProps }) => {
  // Component logic here
  return <ChildComponent {...restProps} />;
};

Enter fullscreen mode Exit fullscreen mode

āĻāĻ‡ example āĻ, āĻĢāĻžāĻ‚āĻļāĻ¨ āĻĒā§āĻ¯āĻ°āĻžāĻŽāĻŋāĻŸāĻžāĻ° āĻ props destructure āĻ•āĻ°ā§‡ āĻ¨ā§‡ā§ŸāĻžāĻ° āĻ•āĻžāĻ°āĻ¨ā§‡ āĻ¸āĻšāĻœā§‡āĻ‡ āĻ†āĻŽāĻ°āĻž āĻŦā§āĻāĻ¤ā§‡ āĻĒāĻžāĻ°āĻŋ āĻāĻ‡ āĻ•āĻŽā§āĻĒā§‹āĻ¨ā§‡āĻ¨ā§āĻŸāĻŸāĻŋ āĻ•āĻŋ āĻ•āĻŋ props āĻāĻ° āĻ‰āĻĒāĻ° dependent.

2. Conditional Rendering with Ternary Operators:

āĻ†āĻŽāĻ°āĻž āĻ¯āĻ–āĻ¨ āĻ•āĻ¨ā§āĻĄāĻŋāĻļāĻ¨āĻžāĻ˛āĻŋ āĻ•ā§‹āĻ¨ āĻ•āĻŽā§āĻĒāĻ¨ā§‡āĻ¨ā§āĻŸ āĻ°ā§‡āĻ¨ā§āĻĄāĻžāĻ° āĻ•āĻ°āĻŋ āĻ¤āĻ–āĻ¨ if-else statement āĻ‡āĻ‰āĻœ āĻ¨āĻž āĻ•āĻ°ā§‡ āĻ†āĻŽāĻ°āĻž ternary āĻ‡āĻ‰āĻœ āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°āĻŋ, āĻāĻ¤ā§‡ āĻ†āĻŽāĻžāĻĻā§‡āĻ° āĻ•ā§‹āĻĄ āĻ…āĻ¨ā§‡āĻ• āĻ•ā§āĻ˛āĻŋāĻ¨ āĻāĻŦāĻ‚ āĻ°āĻŋāĻĄā§‡āĻŦāĻ˛ āĻšāĻŦā§‡ āĨ¤

const MyComponent = ({ isLoggedIn }) => {
  return isLoggedIn ? <UserDashboard /> : <Login />;
};

Enter fullscreen mode Exit fullscreen mode

āĻāĻ‡ example āĻ, āĻ‡āĻ‰āĻœāĻžāĻ° āĻ¯āĻ–āĻ¨ āĻ˛āĻ—āĻŋāĻ¨ āĻ•āĻ°āĻž āĻĨāĻžāĻ•āĻŦā§‡ āĻ¤āĻ–āĻ¨ āĻ†āĻŽāĻ°āĻž UserDashboard āĻāĻŦāĻ‚ āĻ¯āĻ–āĻ¨ āĻ˛āĻ—āĻŋāĻ¨ āĻ¨āĻž āĻĨāĻžāĻ•āĻŦā§‡ āĻ¤āĻ–āĻ¨ Login āĻ•āĻŽā§āĻĒāĻ¨ā§‡āĻ¨ā§āĻŸ āĻ•ā§‡ āĻ°ā§‡āĻ¨ā§āĻĄāĻžāĻ° āĻ•āĻ°āĻŦ ternary operator āĻ‡āĻ‰āĻœ āĻ•āĻ°ā§‡ āĨ¤

3. React Fragments for Cleaner JSX:

āĻ†āĻŽāĻ°āĻž āĻ¯āĻ–āĻ¨ āĻāĻ•āĻŸāĻž āĻ•āĻŽā§āĻĒāĻ¨ā§‡āĻ¨ā§āĻŸ āĻ āĻ†āĻ°āĻ“ āĻ•āĻŋāĻ›ā§ child component āĻ‡āĻ‰āĻœ āĻ•āĻ°āĻŋ āĻ¤āĻ–āĻ¨ āĻ¸ā§‡āĻ—ā§āĻ˛ā§‹ āĻāĻ•āĻŸāĻž wrapper div āĻĻāĻŋā§Ÿā§‡ wrap āĻ¨āĻž āĻ•āĻ°ā§‡ āĻ†āĻŽāĻ°āĻž react fragments āĻ‡āĻ‰āĻœ āĻ•āĻ°ā§‡ āĻ†āĻŽāĻžāĻĻā§‡āĻ° āĻ•ā§‹āĻĄ āĻ†āĻ°ā§‹ āĻŦā§‡āĻļāĻŋ āĻ•ā§āĻ˛āĻŋāĻ¨ āĻāĻŦāĻ‚ āĻ°āĻŋāĻĄā§‡āĻŦāĻ˛ āĻ•āĻ°ā§‡ āĻĢā§‡āĻ˛āĻ¤ā§‡ āĻĒāĻžāĻ°āĻŋ āĨ¤

const MyComponent = () => (
  <>
    <Header />
    <MainContent />
    <Footer />
  </>
);

Enter fullscreen mode Exit fullscreen mode

4. PropTypes and Default Props:

āĻāĻ•āĻŸāĻž component āĻ¯ā§‡ props āĻ—ā§āĻ˛ā§‹ āĻ°āĻŋāĻ¸āĻŋāĻ­ āĻ•āĻ°ā§‡ āĻ¸ā§‡āĻ—ā§āĻ˛ā§‹āĻ° āĻŸāĻžāĻ‡āĻĒ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāĻ° āĻ•āĻ°ā§‡ āĻĻā§‡ā§ŸāĻžāĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§‡ āĻ†āĻ° āĻĄāĻŋāĻĢāĻ˛ā§āĻŸ props āĻ¸ā§‡āĻŸ āĻ•āĻ°ā§‡ āĻ†āĻŽāĻ°āĻž development āĻ¸ā§āĻŸā§‡āĻœ āĻāĻ‡ āĻ†āĻŽāĻžāĻĻā§‡āĻ° āĻāĻ°āĻ° āĻ—ā§āĻ˛ā§‹ āĻĄāĻŋāĻŸā§‡āĻ•ā§āĻŸ āĻāĻŦāĻ‚ āĻšā§āĻ¯āĻžāĻ¨ā§āĻĄāĻ˛ āĻ•āĻ°ā§‡ āĻĢā§‡āĻ˛āĻ¤ā§‡ āĻĒāĻžāĻ°āĻŋ āĨ¤ āĻāĻ¤ā§‡ āĻ†āĻŽāĻžāĻĻā§‡āĻ° development āĻŸāĻžāĻ‡āĻŽ āĻ…āĻ¨ā§‡āĻ• āĻ•āĻŽā§‡ āĻ†āĻ¸ā§‡ āĨ¤

import PropTypes from 'prop-types';

const MyComponent = ({ name, age }) => {
  // Component logic here
};

MyComponent.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number,
};

MyComponent.defaultProps = {
  age: 25,
};

Enter fullscreen mode Exit fullscreen mode

āĻāĻ›āĻžā§œāĻžāĻ“ āĻāĻ°āĻ•āĻŽ āĻ†āĻ°ā§‹ āĻ…āĻ¨ā§‡āĻ• āĻŦā§‡āĻ¸ā§āĻŸ āĻĒā§āĻ°ā§āĻ¯āĻ•ā§āĻŸāĻŋāĻ¸ āĻ†āĻ›ā§‡ āĻ¯āĻž āĻĢāĻ˛ā§‹ āĻ•āĻ°ā§‡ āĻ†āĻŽāĻ°āĻž āĻ†āĻŽāĻžāĻĻā§‡āĻ° āĻ†āĻŽāĻžāĻĻā§‡āĻ° āĻ•ā§‹āĻĄ āĻŦā§‡āĻœ āĻ•ā§‡ āĻ•ā§āĻ˛āĻŋāĻ¨, āĻ°āĻŋāĻĄā§‡āĻŦāĻ˛ āĻāĻŦāĻ‚ Efficient āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°āĻŋ āĨ¤ āĻ§āĻ¨ā§āĻ¯āĻŦāĻžāĻĻ āĨ¤

bestpractice Article's
30 articles in total
Favicon
From Bi-weekly to Every 5 Minutes: Modern Continuous Deployment Strategies
Favicon
NotaciÃŗn Big O - Python
Favicon
Docker Advance Part 2: Docker Logging
Favicon
Client Extension no Liferay
Favicon
Dockerfile Best Practices: How to Create Efficient Containers
Favicon
Microservice Best Practices: Scale your java microservices using virtual threads & async programming
Favicon
Design Patterns for C
Favicon
Mastering React Hooks: Best Practices for Efficient and Maintainable Code
Favicon
Why You Should End Your Source Files With a New Line
Favicon
Puppet best practice
Favicon
@Nullable et @NonNull
Favicon
Component best practice question.
Favicon
How to Use CodeWhisperer to Identify Issues and Use Suggestions to Improve Code Security in your IDE
Favicon
Mastering React: Best Practices for Cleaner and More Efficient Code
Favicon
AWS Well-Architected Review in Action
Favicon
Improving Code Quality in Java: Best Practices and Examples
Favicon
Mastering JavaScript Event Handling for Enhanced Frontend Functionality
Favicon
TIL: Best Practices for Handling Secret Keys in Sinatra - The Do's and Don'ts
Favicon
Enhancing Website Accessibility: A Guide for Supporting Users with Disabilities
Favicon
Proposal for a framework.json file in Angular applications
Favicon
Part 3: Component Structure - Building Reusable and Maintainable Components in React!
Favicon
Using useReducer and Redux Toolkit Together: A Powerful Combination for State Management
Favicon
Separation of concerns in React and React Native.
Favicon
REST-API Design Best Practices
Favicon
Flags In Programming
Favicon
10 Essential Best Practices for Writing High-Quality C++ Source Code
Favicon
Avoiding code duplication in styled components
Favicon
Es mala prÃĄctica renderizar JSX en React Hook?
Favicon
ReactJS Best Practices
Favicon
Why you should adopt Makefile in all of your projects

Featured ones: