SlideShare a Scribd company logo
3
Most read
4
Most read
6
Most read
REACT RENDER PROP
By Saikat Samanta
Knowledge required:
1. Basic of React Js.
2. Typescript
Point to note:
1. In this presentation we are going to use typescript to create the examples.
2
What is render prop?
The term “render prop” refers to a technique for sharing code between React
components using a prop whose value is a function.
So, we have two things here:
1. Sharing code by using prop.
2. Prop value will be a function.
Let’s simplify it...
3
Sharing code by using Prop
// Child component
export function Component(props: { name: JSX.Element})
{
return <div>{props.name}</div>;
}
// Parent component
export default function App() {
return (
<div className="App">
<Component name={<p>John</p>} />
</div>
);
}
4
Prop value will be a function
// Child component
export function Component(props: { name: () => JSX.Element }) {
return <div>{props.name()}</div>;
}
// Parent component
export default function App() {
return (
<div className="App">
<Component name={() => <p>John</p>} />
</div>
);
}
5
Prop value will send from child component
// Child component
export function Component(props: { name: (name: string ) => JSX.Element }) {
return <div>{props.name("John")}</div>;
}
// Parent component
export default function App() {
return (
<div className="App">
<Component name={(name) => <p>{name}</p>} />
</div>
);
}
6
Now, we have the some idea about, what render prop definition mean.
So, it’s saying, we can share code between react components with the help of prop and
prop value will be a function.
Let’s see how render prop help to share
code between components. 🤔
7
Let’s take an example:
We have two tasks:
1. We need to implement a component which will count how many times a button
clicked.
2. We have a header and it will count how many times we hover on it.
8
1. Button click counter:
// Child component
export function ClickCounter() {
const [count, setCount] = useState(0);
const addCount = () => {
setCount(prevCount => prevCount + 1);
}
return (
<div>
<button onClick={addCount}>
You click {count} time
</button>
</div>
);
}
// Parent component
export default function App() {
return (
<div className="App">
<ClickCounter/>
</div>
);
}
repeated code
9
2. Hover counter
// Child component
export function HoverCounter() {
const [count, setCount] = useState(0);
const addCount = () => {
setCount(prevCount => prevCount + 1);
}
return (
<div onMouseEnter={addCount}>
<p>Hover {count} times</p>
</div>
);
}
// Parent component
export default function App() {
return (
<div className="App">
<HoverCounter/>
</div>
);
}
repeated code
10
Here, we are repeating a code block in both component. To avoid this situation `render
prop`comes into picture.
By using `render prop` we will share the same piece of code with both components.
Let’s see … 🤔
11
Remove the repeated part:
Removed the repeated part and replace with props in ClickCounter.
export function ClickCounter({
count,
onClick,
}: {
count: number;
onClick: React.MouseEventHandler<HTMLButtonElement>;
}) {
return (
<div>
<button onClick={onClick}>You click {count} time</button>
</div>
);
}
12
Remove the repeated part:
Removed the repeated part and replace with props in HoverCounter.
export function HoverCounter({
count,
onMouseHover,
}: {
count: number;
onMouseHover: React.MouseEventHandler<HTMLDivElement>;
}) {
return (
<div onMouseEnter={onMouseHover}>
<p>Hover {count} times</p>
</div>
);
}
13
Create a common counter component:
export function Counter(props: {
render: (count: number, addCount: () => void) => JSX.Element;
}) {
// Count logic
const [count, setcount] = useState(0);
const addCount = () => {
setcount((prevState) => prevState + 1);
};
// End logic
return <div>{props.render(count, addCount)}</div>;
}
14
Let’s use the common counter:
Let’s use the common counter component in parent component.
export default function App() {
return (
<div className="App">
<Counter
render={(count, setCount) => (
<ClickCounter count={count} onClick={setCount} />
)}
/>
<Counter
render={(count, setCount) => (
<HoverCounter count={count} onMouseHover={setCount} />
)}
/>
</div>
);
}
15
Result:
Here, we don’t repeat the same code but both components are working as expected.
16
Conclusion:
1. `Render props` will be very helpful to avoid code repetition.
2. We can share code in any level of component tree no need to be a direct child.
3. This will help to write code in more organized way without code repetition.
17
Reference:
1. https://reactjs.org/docs/render-props.html
2. https://blog.logrocket.com/react-reference-guide-render-props/
3. https://flexiple.com/react/render-props-an-advanced-react-pattern/
18
Question? ♂️
19
Thank you...
20

More Related Content

PPTX
React state
PDF
An introduction to React.js
PPTX
PPTX
[Final] ReactJS presentation
PPTX
What is component in reactjs
PPTX
Intro to React
PDF
React JS - Introduction
PPTX
React js programming concept
React state
An introduction to React.js
[Final] ReactJS presentation
What is component in reactjs
Intro to React
React JS - Introduction
React js programming concept

What's hot (20)

PDF
React js
PPTX
React js for beginners
PDF
3. Java Script
PPTX
ReactJS presentation.pptx
PPTX
Its time to React.js
PPTX
React hooks
PDF
Angular Advanced Routing
PDF
ES6 presentation
PPTX
jQuery PPT
PDF
JavaScript - Chapter 8 - Objects
PPTX
React JS - A quick introduction tutorial
ODP
Basics of VueJS
PPTX
Javascript functions
PPTX
PDF
ES2015 / ES6: Basics of modern Javascript
PPTX
Introduction to React
PPT
JavaScript - An Introduction
PPTX
PPTX
React.js - The Dawn of Virtual DOM
React js
React js for beginners
3. Java Script
ReactJS presentation.pptx
Its time to React.js
React hooks
Angular Advanced Routing
ES6 presentation
jQuery PPT
JavaScript - Chapter 8 - Objects
React JS - A quick introduction tutorial
Basics of VueJS
Javascript functions
ES2015 / ES6: Basics of modern Javascript
Introduction to React
JavaScript - An Introduction
React.js - The Dawn of Virtual DOM
Ad

Similar to React render props (20)

PDF
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
PPTX
Angular2 + rxjs
PDF
[FEConf Korea 2017]Angular 컴포넌트 대화법
PDF
React: JSX and Top Level API
PDF
2018 05-16 Evolving Technologies: React, Babel & Webpack
PPTX
Ways to Set Focus on an Input Field After Rendering in React.pptx
PDF
Angular JS2 Training Session #2
PDF
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
PDF
Advanced React Component Patterns - ReactNext 2018
PDF
Introduction to React for Frontend Developers
PPTX
React & Redux for noobs
PDF
Workshop 23: ReactJS, React & Redux testing
PDF
Integrating React.js with PHP projects
PDF
3 Simple Steps to follow to Create React JS Components
PDF
Server side rendering with React and Symfony
PPTX
An introduction to Vue.js
PDF
React native app with type script tutorial
PPTX
React native introduction
PDF
React redux
PPTX
React outbox
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Angular2 + rxjs
[FEConf Korea 2017]Angular 컴포넌트 대화법
React: JSX and Top Level API
2018 05-16 Evolving Technologies: React, Babel & Webpack
Ways to Set Focus on an Input Field After Rendering in React.pptx
Angular JS2 Training Session #2
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
Advanced React Component Patterns - ReactNext 2018
Introduction to React for Frontend Developers
React & Redux for noobs
Workshop 23: ReactJS, React & Redux testing
Integrating React.js with PHP projects
3 Simple Steps to follow to Create React JS Components
Server side rendering with React and Symfony
An introduction to Vue.js
React native app with type script tutorial
React native introduction
React redux
React outbox
Ad

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Approach and Philosophy of On baking technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
MYSQL Presentation for SQL database connectivity
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Tartificialntelligence_presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Approach and Philosophy of On baking technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
Machine Learning_overview_presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Dropbox Q2 2025 Financial Results & Investor Presentation
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release
Tartificialntelligence_presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...

React render props

  • 1. REACT RENDER PROP By Saikat Samanta
  • 2. Knowledge required: 1. Basic of React Js. 2. Typescript Point to note: 1. In this presentation we are going to use typescript to create the examples. 2
  • 3. What is render prop? The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function. So, we have two things here: 1. Sharing code by using prop. 2. Prop value will be a function. Let’s simplify it... 3
  • 4. Sharing code by using Prop // Child component export function Component(props: { name: JSX.Element}) { return <div>{props.name}</div>; } // Parent component export default function App() { return ( <div className="App"> <Component name={<p>John</p>} /> </div> ); } 4
  • 5. Prop value will be a function // Child component export function Component(props: { name: () => JSX.Element }) { return <div>{props.name()}</div>; } // Parent component export default function App() { return ( <div className="App"> <Component name={() => <p>John</p>} /> </div> ); } 5
  • 6. Prop value will send from child component // Child component export function Component(props: { name: (name: string ) => JSX.Element }) { return <div>{props.name("John")}</div>; } // Parent component export default function App() { return ( <div className="App"> <Component name={(name) => <p>{name}</p>} /> </div> ); } 6
  • 7. Now, we have the some idea about, what render prop definition mean. So, it’s saying, we can share code between react components with the help of prop and prop value will be a function. Let’s see how render prop help to share code between components. 🤔 7
  • 8. Let’s take an example: We have two tasks: 1. We need to implement a component which will count how many times a button clicked. 2. We have a header and it will count how many times we hover on it. 8
  • 9. 1. Button click counter: // Child component export function ClickCounter() { const [count, setCount] = useState(0); const addCount = () => { setCount(prevCount => prevCount + 1); } return ( <div> <button onClick={addCount}> You click {count} time </button> </div> ); } // Parent component export default function App() { return ( <div className="App"> <ClickCounter/> </div> ); } repeated code 9
  • 10. 2. Hover counter // Child component export function HoverCounter() { const [count, setCount] = useState(0); const addCount = () => { setCount(prevCount => prevCount + 1); } return ( <div onMouseEnter={addCount}> <p>Hover {count} times</p> </div> ); } // Parent component export default function App() { return ( <div className="App"> <HoverCounter/> </div> ); } repeated code 10
  • 11. Here, we are repeating a code block in both component. To avoid this situation `render prop`comes into picture. By using `render prop` we will share the same piece of code with both components. Let’s see … 🤔 11
  • 12. Remove the repeated part: Removed the repeated part and replace with props in ClickCounter. export function ClickCounter({ count, onClick, }: { count: number; onClick: React.MouseEventHandler<HTMLButtonElement>; }) { return ( <div> <button onClick={onClick}>You click {count} time</button> </div> ); } 12
  • 13. Remove the repeated part: Removed the repeated part and replace with props in HoverCounter. export function HoverCounter({ count, onMouseHover, }: { count: number; onMouseHover: React.MouseEventHandler<HTMLDivElement>; }) { return ( <div onMouseEnter={onMouseHover}> <p>Hover {count} times</p> </div> ); } 13
  • 14. Create a common counter component: export function Counter(props: { render: (count: number, addCount: () => void) => JSX.Element; }) { // Count logic const [count, setcount] = useState(0); const addCount = () => { setcount((prevState) => prevState + 1); }; // End logic return <div>{props.render(count, addCount)}</div>; } 14
  • 15. Let’s use the common counter: Let’s use the common counter component in parent component. export default function App() { return ( <div className="App"> <Counter render={(count, setCount) => ( <ClickCounter count={count} onClick={setCount} /> )} /> <Counter render={(count, setCount) => ( <HoverCounter count={count} onMouseHover={setCount} /> )} /> </div> ); } 15
  • 16. Result: Here, we don’t repeat the same code but both components are working as expected. 16
  • 17. Conclusion: 1. `Render props` will be very helpful to avoid code repetition. 2. We can share code in any level of component tree no need to be a direct child. 3. This will help to write code in more organized way without code repetition. 17