← react hacks / react / destructuring-props-in-functional-components.md

Destructuring Props in React Functional Components

Discover how to use destructuring props in React functional components to write cleaner and more efficient code.

Destructuring in JavaScript helps us write cleaner code by allowing us to extract multiple properties from an object or array in a single statement. In React, we use destructuring in functional components to handle props.

Here’s how you might do it without destructuring:

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

Now, let’s use destructuring to achieve the same result:

function Greeting({ name }) {
  return <h1>Hello, {name}</h1>;
}

In the second example, we’re destructuring name directly from the props. This allows us to use name directly in the component, making our code cleaner and easier to read.

Remember, when using destructuring with functional components, you can destructure as many props as you need in the function parameters. This will make your code efficient and clean.

Keep experimenting!

Quick hacks and tips from my daily workflow. Found this useful? Let me know.

@samuellawrentz →

$ ls ../react | head -4

More hacks

cd ../react →
  1. c52b973 Harnessing the Power of useRef Hook in React

    tag: reacttag: hookstag: useRef

  2. 8150376 Adding Event Handlers in JSX

    tag: reacttag: jsxtag: event handlers

  3. 2c098c3 Avoiding Unnecessary Re-renders with React.memo

    tag: reacttag: web developmenttag: performance optimization

  4. c2ffa3b Optimizing React Apps with useCallback

    tag: reacttag: useCallbacktag: optimization

00:00

This helps me increase the session time of my site. Thank you!

Can you stay a bit longer?