Skip to content

Understanding Props in React: the D.R.U. Technique

Props felt strange and hard to grasp when I first met them. D.R.U. (Define, Receive, Use) is the acronym I came up with to make them click.

NevilleFrontend engineer
· 2 min read

A few years ago, when I was first introduced to the concept of props in React, it seemed strange and difficult to grasp. It took quite a while and a lot of practice to really understand what props are and how they work. Along the way I came up with an acronym, D.R.U., that can help you understand props in React easily.

What are props?

Props is short for properties. Props are how a parent component passes data to a child component in the component tree. Data in React flows one way, downwards, and props are what carry it.

Props are very similar to HTML attributes, such as className, onClick or type on a <button> element. The difference is that you decide what they are called and what they hold: strings, numbers, objects, functions, or even other components.

The D.R.U. technique

D.R.U. stands for:

  • D: Define the prop's value in the parent component.
  • R: Receive the prop in the child component.
  • U: Use the prop in the child component.
Data flows down the tree: the parent defines it, each child receives and uses it.

When you build a React application, the components form a tree, and data flows from the top to the bottom. Each time data moves one level down, it travels as a prop. D.R.U. gives you a structure for the three steps needed to pass a prop down and use it.

An example

App.jsx
// Parent component
function App() {
  const productName = "Car"; // D: Define the prop value
 
  return <Product name={productName} />;
}
 
// Child component
function Product({ name }) {
  // R: Receive the prop (here, by destructuring it)
  return <h1>This is a {name}</h1>; // U: Use the prop
}

As you can see above, we first Define the value (productName) in the parent component and pass it to Product under a name of our choice, in this case name. Product then Receives the prop through its parameter, and finally Uses name in the JSX it returns.

React's children prop

children is a special prop. When you nest content between the opening and closing tags of a component, the component receives that content in a prop called children.

Card.jsx
function Card({ children }) {
  return <div className="card">{children}</div>;
}
 
function App() {
  return (
    <Card>
      <Product name="Car" />
    </Card>
  );
}

Here, <Product name="Car" /> is passed to Card as its children. You can think of a component with a children prop as having a "hole" that its parent can fill with any JSX. You will often use children for visual wrappers: containers, panels, grids and so on.

Default values

You can give a prop a default value when you destructure it, by putting = and the value right after its name. It's used whenever the parent doesn't pass that prop (or passes undefined).

Product.jsx
function Product({ name = "Bicycle" }) {
  return <h1>This is a {name}</h1>;
}
 
<Product />; // This is a Bicycle

If no value is passed for name, Bicycle is used.

Conclusion

This article has explained how props work in React. Breaking them down with the D.R.U. technique (Define, Receive, Use) will help you reason about where every value in your components comes from. Once props feel natural, read about prop drilling, the problem that shows up when props have to travel through too many layers.

Good luck! 👋🏻

Written by Neville

Geologist turned frontend engineer from Cameroon, based in Ghana. Makes web and mobile apps faster and writes down how. More about me

Liked this? Get the next one.

One deep dive a month. No spam, unsubscribe in one click.