Skip to main content

Command Palette

Search for a command to run...

React Props and Key

Published
•3 min read•View as Markdown
React Props and Key
R

Passionate Developer | Lover of coding and problem-solving | Constantly learning and growing | Let's build something amazing!

H

Passionate Developer | Lover of coding and problem-solving | Constantly learning and growing | Let's build something amazing!

React Props

Props stands for properties.

Props are arguments passed into React components. so passing props to a React component is like passing arguments to a JS function.

Props are passed to components via HTML attributes.

Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, and functions.

When we have to dynamically pass a data to an component, we pass it as props.

Passing props to a component

React takes all the props given to the component and wraps them up into an object and passes it to that component as an argument called props.

So the props are given to a component in the form of attributes but the props passed as an argument to that component is a JS object .

If you have a variable to send, and not a string as in the example above, you just put the variable name inside curly brackets.

function Car(props) {                     // here props is a js object 
    return <h2>I am a { props.brand } named {props.name}!</h2>;
  }

  function Garage() {
    const carName = "Ford";
    return (
      <>
          <h1>Who lives in my garage?</h1>
          <Car brand={ carName } name = "abc"/>       // here props are passed as attributes
      </>
    );
  }

  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(<Garage />);

Destructuring of props

Destructuring of props is possible since props is a JS object .
It allows the prop attributes to be used as variables.

Method 1 :

function Car({brand,name}) {          //destructuring
    return <h2>I am a { brand } named {name}!</h2>;
  }

  function Garage() {

    return (
      <>
          <h1>Who lives in my garage?</h1>
          <Car brand="Ford" name = "abc"/>
      </>
    );
  }

  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(<Garage />);

Method 2:

function Car(props) {
    const {brand , name} = props;               // destructuring
    return <h2>I am a { brand } named {name}!</h2>;
  }

  function Garage() {

    return (
      <>
          <h1>Who lives in my garage?</h1>
          <Car brand="Ford" name = "abc"/>
      </>
    );
  }

  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(<Garage />);

Important Fact !

Use of Props implements UI Driven Config.
It is UI driven by the config (incoming data). So the UI changes according to the data passed (in form of props).


KEYS

In React, keys are special string attributes you need to include when creating lists of elements. They help React identify which items have changed, are added, or are removed. This is crucial for efficient rendering and updating of the UI.

When there are some components at the same level then these components need to be represented by using a key which uniquely identifies these components
So that during looping, the re-rendering of all the components is avoided when a new component is added.

const todoItems = todos.map((todo) =>
  <li key={todo.id}>
    {todo.text}
  </li>
);

Note: Don’t use index as the key (becoz the order of elements can change). So use them only when you don’t have any stable uniquely differentiating ids.

const todoItems = todos.map((todo, index) =>
  <li key={index}>
    {todo.text}
  </li>
);

React JS

Part 4 of 10

This series is a documentation of my learning journey of React. Each article is crafted to break down the concepts I explore, from the basics of JSX and components to advanced features like hooks, state management, and performance optimization.

Up next

React Fiber

Before we start, Restructuring the code Make different files for each component . Don’t add hardcoded data in the component files, make different utility folder for that. Now to use these components in the main app.js file or in other components file...

More from this blog

T

TechStack Tales

17 posts

It is dedicated to sharing my journey as a developer. Here, I document my learnings, insights, and experiences, making it a resource for both beginners and enthusiasts.