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

What's hot (20)

PPTX
React JS: A Secret Preview
valuebound
 
PPTX
jQuery PPT
Dominic Arrojado
 
PPTX
Introduction to React JS for beginners | Namespace IT
namespaceit
 
ODP
Introduction to ReactJS
Knoldus Inc.
 
PPTX
Event In JavaScript
ShahDhruv21
 
ODP
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
PPTX
Intro to React
Justin Reock
 
PDF
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
PDF
Nodejs presentation
Arvind Devaraj
 
PDF
Android ui dialog
Krazy Koder
 
PPTX
React + Redux Introduction
Nikolaus Graf
 
PPTX
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
PPTX
Reactjs
Mallikarjuna G D
 
PPTX
React js programming concept
Tariqul islam
 
PDF
ES6 presentation
ritika1
 
PPTX
Introduction to React JS
Arnold Asllani
 
PPTX
Angular 9
Raja Vishnu
 
PDF
Lets make a better react form
Yao Nien Chung
 
PPTX
ReactJs
Sahana Banerjee
 
React JS: A Secret Preview
valuebound
 
jQuery PPT
Dominic Arrojado
 
Introduction to React JS for beginners | Namespace IT
namespaceit
 
Introduction to ReactJS
Knoldus Inc.
 
Event In JavaScript
ShahDhruv21
 
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
Intro to React
Justin Reock
 
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
Nodejs presentation
Arvind Devaraj
 
Android ui dialog
Krazy Koder
 
React + Redux Introduction
Nikolaus Graf
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
React js programming concept
Tariqul islam
 
ES6 presentation
ritika1
 
Introduction to React JS
Arnold Asllani
 
Angular 9
Raja Vishnu
 
Lets make a better react form
Yao Nien Chung
 

Similar to React render props (20)

PDF
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
PPTX
Angular2 + rxjs
Christoffer Noring
 
PDF
[FEConf Korea 2017]Angular 컴포넌트 대화법
Jeado Ko
 
PDF
React: JSX and Top Level API
Fabio Biondi
 
PDF
2018 05-16 Evolving Technologies: React, Babel & Webpack
Codifly
 
PPTX
Ways to Set Focus on an Input Field After Rendering in React.pptx
BOSC Tech Labs
 
PDF
Angular JS2 Training Session #2
Paras Mendiratta
 
PDF
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
Edureka!
 
PDF
Advanced React Component Patterns - ReactNext 2018
Robert Herbst
 
PDF
Introduction to React for Frontend Developers
Sergio Nakamura
 
PPTX
React & Redux for noobs
[T]echdencias
 
PDF
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
PDF
Integrating React.js with PHP projects
Ignacio Martín
 
PDF
3 Simple Steps to follow to Create React JS Components
Surendra kumar
 
PDF
Server side rendering with React and Symfony
Ignacio Martín
 
PPTX
An introduction to Vue.js
TO THE NEW Pvt. Ltd.
 
PDF
React native app with type script tutorial
Katy Slemon
 
PPTX
React native introduction
InnerFood
 
PDF
React redux
Michel Perez
 
PPTX
React outbox
Angela Lehru
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
Angular2 + rxjs
Christoffer Noring
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
Jeado Ko
 
React: JSX and Top Level API
Fabio Biondi
 
2018 05-16 Evolving Technologies: React, Babel & Webpack
Codifly
 
Ways to Set Focus on an Input Field After Rendering in React.pptx
BOSC Tech Labs
 
Angular JS2 Training Session #2
Paras Mendiratta
 
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
Edureka!
 
Advanced React Component Patterns - ReactNext 2018
Robert Herbst
 
Introduction to React for Frontend Developers
Sergio Nakamura
 
React & Redux for noobs
[T]echdencias
 
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
Integrating React.js with PHP projects
Ignacio Martín
 
3 Simple Steps to follow to Create React JS Components
Surendra kumar
 
Server side rendering with React and Symfony
Ignacio Martín
 
An introduction to Vue.js
TO THE NEW Pvt. Ltd.
 
React native app with type script tutorial
Katy Slemon
 
React native introduction
InnerFood
 
React redux
Michel Perez
 
React outbox
Angela Lehru
 
Ad

Recently uploaded (20)

PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Practical Applications of AI in Local Government
OnBoard
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
Ad

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