DEV Community

Cover image for render prop and children in react
aravind_akay
aravind_akay

Posted on

2 1

render prop and children in react

render prop

Hello guys I want to give an introduction about what render prop is.

According to the official documentation

The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function.

From my point of view its just an another prop but it will share some of the code but you can name it whatever you want as prop.

Foe an example,

import "./styles.css";
import ListItem from "./ListItem";

export default function App() {
  return (
    <div className="App">
      <ListItem render={"Simply a prop"} />

    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

The output is

Image description

Traditionally it has been used as this following way,

In App.js
<ListItem render={(data) => <li>{data}</li>}  data="Simply a prop"/>

In ListItem.js
const ListItem = (props) => {
  return <div> {props.render(props.data)}</div>;
};
Enter fullscreen mode Exit fullscreen mode

The output is

Image description

I think that solves your doubts with render prop.

children prop

children prop is nothing but the ui rendered between the elements. what do I mean by that. Lets say you have a div in between you write a lot of code , all the codes you've written between that div is children to that div.

Apply the same logic for components.

For example consider the same ListItem component,

//In App.js

<ListItem> <div>Hello Guys , this is considered as the children </div> </ListItem>

//In ListItem.js

const ListItem = (props) => {
  return <div> {props.children}</div>;
};

Enter fullscreen mode Exit fullscreen mode

The output is

Image description

I think you should try this example and comment if there is any doubt. Happy coding.

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay