SlideShare a Scribd company logo
1 of 22
React Patterns
Naimish Verma - 7th November, 2020
https://naimishverma.in
Topics to be covered-
1. React’s Compositional Nature
2. Higher Order Components
3. Render Props
4. Presentational and Container components
5. Child Aware components
6. Child Enhancing components
React’s Compositional Nature
One of the biggest benefits of react is it’s composability.
React has a powerful composition model, and we recommend using
composition instead of inheritance to reuse code between components.
- React official documentation
Consider a example -
<App/>
|
<Header/>
|
<Navigation/>
The trivial approach for combining these components is to reference them
where we need them
This approach introduces a couple of
problems, what if we need the same
header but without navigation. We can’t
achieve that because both of them are
tightly coupled with each other.
Also it’s hard to test there type of
components.
React’s Children API
We can now test this
component because we may
render Header with an empty
div. This will isolate the
component and will let us
focus on one piece of
application.
Higher Order Components(HOC)
It’s just a function which accepts an original component and returns an
enhanced component.
The actual creation of the higher order component should happen at
component definition level. Or in other words it’s a bad practise to do inside
another react component because it maybe slow and lead to performance
issues.
- Dan Abramov
It is slow and blows away the DOM and state completely in all components below OriginalComponent
because enhanceComponent(OriginalComponent) gives you a different type every time you render().
We just do the this once so render() always returns same type of EnhanceComponent.
Problem -
Solution -
Render Props
The term “render prop” refers to a technique for sharing code
between React components using a prop whose value is a
function.
Render props provide flexibility and
help us to reuse the code. In fact,
any prop that is a function that a
component uses to know what to
render is technically a “render
prop”.
Presentational and Container Components
The main question is where should I put all my data, or how to
communicate changes or how to manage state?
The best approach is “DATA DOWN AND ACTIONS UP”. This is what
presentational and container components all about.
Example:
Problems in this approach
1. Firstly, the clock component mutates the state itself. If any other part
of application depends on this data, it will be difficult to share it.
2. _formatTime is actually doing two things - it extracts the needed
information from date object and makes sure that values are always
presented in two digits.
Solution:
Extracting the component into presentation and container component.
Container knows about data, its shape and where it comes from. They
know about how the things work or the so called business logic. The
presentational components on the other hand deals only with the UI part
irrespective from the business logic.
The concept of container
and presentation is not
new at all, but it fits nicely
with React. It makes our
application better
structured, easy to
manage and scalable.
Child Enhancing Components
These are the type of components which takes their children component
and adds some extra functionality on top of it.
Let’s have a look by an example.
Clone and return a new React element using
element as the starting point. The resulting element
will have the original element’s props with the new
props merged in shallowly. New children will replace
existing children. key and ref from the original
element will be preserved.
Clone element API
Child Aware Component
Victory Charts
Interpretation
It seems like that these are bidirectional components, where child
components are communicating back to the parent component.
Now it conflicts with our earlier approach of “Data Down and Actions
Up”.
Interrogating it’s children components
It turns out that the VictoryChart Component is interrogating with its child
component.
Each child component exposes its data via props and selectors and its passed
through the parent component. So, the parent component can interpret that data
and can act accordingly.
This is something called as Child Aware Component.
References
1. React In Patterns - Krasimir Tsonev
2. Reactjs.org
3. Robert Herbst - Advanced Patterns| ReactNext 2018
Thank
you.

More Related Content

Similar to React patterns

Functional Components in Vue.js
Functional Components in Vue.jsFunctional Components in Vue.js
Functional Components in Vue.jsAustin Gil
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & AnswerMildain Solutions
 
Everything to Know About React Re-Rendering: A Comprehensive Guide
Everything to Know About React Re-Rendering: A Comprehensive GuideEverything to Know About React Re-Rendering: A Comprehensive Guide
Everything to Know About React Re-Rendering: A Comprehensive GuideBOSC Tech Labs
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?BOSC Tech Labs
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react jsStephieJohn
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)Chiew Carol
 
Props concepts in React JS Course for beginners .docx
Props concepts in React JS Course for beginners .docxProps concepts in React JS Course for beginners .docx
Props concepts in React JS Course for beginners .docxReact Masters
 
What are Props Concept in React JS Course
What are Props Concept in React JS CourseWhat are Props Concept in React JS Course
What are Props Concept in React JS CourseReact Masters
 
what is context API and How it works in React.pptx
what is context API and How it works in React.pptxwhat is context API and How it works in React.pptx
what is context API and How it works in React.pptxBOSC Tech Labs
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdfArthyR3
 
The virtual DOM and how react uses it internally
The virtual DOM and how react uses it internallyThe virtual DOM and how react uses it internally
The virtual DOM and how react uses it internallyClóvis Neto
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionVisual Engineering
 
How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react MoonTechnolabsPvtLtd
 
React Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfReact Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfBOSC Tech Labs
 

Similar to React patterns (20)

Functional Components in Vue.js
Functional Components in Vue.jsFunctional Components in Vue.js
Functional Components in Vue.js
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
Everything to Know About React Re-Rendering: A Comprehensive Guide
Everything to Know About React Re-Rendering: A Comprehensive GuideEverything to Know About React Re-Rendering: A Comprehensive Guide
Everything to Know About React Re-Rendering: A Comprehensive Guide
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
ReactJs
ReactJsReactJs
ReactJs
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React a11y-csun
React a11y-csunReact a11y-csun
React a11y-csun
 
SharePoint Framework y React
SharePoint Framework y ReactSharePoint Framework y React
SharePoint Framework y React
 
React
ReactReact
React
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
Props concepts in React JS Course for beginners .docx
Props concepts in React JS Course for beginners .docxProps concepts in React JS Course for beginners .docx
Props concepts in React JS Course for beginners .docx
 
What are Props Concept in React JS Course
What are Props Concept in React JS CourseWhat are Props Concept in React JS Course
What are Props Concept in React JS Course
 
what is context API and How it works in React.pptx
what is context API and How it works in React.pptxwhat is context API and How it works in React.pptx
what is context API and How it works in React.pptx
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
The virtual DOM and how react uses it internally
The virtual DOM and how react uses it internallyThe virtual DOM and how react uses it internally
The virtual DOM and how react uses it internally
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react
 
React Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfReact Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdf
 
Angular2 and You
Angular2 and YouAngular2 and You
Angular2 and You
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

React patterns

  • 1. React Patterns Naimish Verma - 7th November, 2020 https://naimishverma.in
  • 2. Topics to be covered- 1. React’s Compositional Nature 2. Higher Order Components 3. Render Props 4. Presentational and Container components 5. Child Aware components 6. Child Enhancing components
  • 3. React’s Compositional Nature One of the biggest benefits of react is it’s composability. React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components. - React official documentation
  • 4. Consider a example - <App/> | <Header/> | <Navigation/> The trivial approach for combining these components is to reference them where we need them
  • 5. This approach introduces a couple of problems, what if we need the same header but without navigation. We can’t achieve that because both of them are tightly coupled with each other. Also it’s hard to test there type of components.
  • 6. React’s Children API We can now test this component because we may render Header with an empty div. This will isolate the component and will let us focus on one piece of application.
  • 7. Higher Order Components(HOC) It’s just a function which accepts an original component and returns an enhanced component.
  • 8. The actual creation of the higher order component should happen at component definition level. Or in other words it’s a bad practise to do inside another react component because it maybe slow and lead to performance issues. - Dan Abramov
  • 9. It is slow and blows away the DOM and state completely in all components below OriginalComponent because enhanceComponent(OriginalComponent) gives you a different type every time you render(). We just do the this once so render() always returns same type of EnhanceComponent. Problem - Solution -
  • 10. Render Props The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function.
  • 11. Render props provide flexibility and help us to reuse the code. In fact, any prop that is a function that a component uses to know what to render is technically a “render prop”.
  • 12. Presentational and Container Components The main question is where should I put all my data, or how to communicate changes or how to manage state? The best approach is “DATA DOWN AND ACTIONS UP”. This is what presentational and container components all about.
  • 14. Problems in this approach 1. Firstly, the clock component mutates the state itself. If any other part of application depends on this data, it will be difficult to share it. 2. _formatTime is actually doing two things - it extracts the needed information from date object and makes sure that values are always presented in two digits.
  • 15. Solution: Extracting the component into presentation and container component. Container knows about data, its shape and where it comes from. They know about how the things work or the so called business logic. The presentational components on the other hand deals only with the UI part irrespective from the business logic.
  • 16. The concept of container and presentation is not new at all, but it fits nicely with React. It makes our application better structured, easy to manage and scalable.
  • 17. Child Enhancing Components These are the type of components which takes their children component and adds some extra functionality on top of it. Let’s have a look by an example.
  • 18. Clone and return a new React element using element as the starting point. The resulting element will have the original element’s props with the new props merged in shallowly. New children will replace existing children. key and ref from the original element will be preserved. Clone element API
  • 20. Interpretation It seems like that these are bidirectional components, where child components are communicating back to the parent component. Now it conflicts with our earlier approach of “Data Down and Actions Up”.
  • 21. Interrogating it’s children components It turns out that the VictoryChart Component is interrogating with its child component. Each child component exposes its data via props and selectors and its passed through the parent component. So, the parent component can interpret that data and can act accordingly. This is something called as Child Aware Component.
  • 22. References 1. React In Patterns - Krasimir Tsonev 2. Reactjs.org 3. Robert Herbst - Advanced Patterns| ReactNext 2018 Thank you.