SlideShare a Scribd company logo
1 of 20
Download to read offline
Design Patterns
in React
27 June, 2019
Tomasz Bąk
tb@tomaszbak.com
Agenda
● Why we need software design patterns?
● Principles of Functional Programming (FP)
● Design patterns in React
What are software design patterns?
● a description or template for how to solve a problem
● they fit between the programming paradigm and a concrete
algorithm
Programming paradigm
(i.e. OOP, functional)
Design patterns
(i.e. Container Pattern)
Algorithm
(i.e. your components)
Why we need software design patterns?
● to understand the high-level design of the code
● to apply proven solutions to common problems
It is often the case that you won’t need to refactor the code
later on because applying the correct design pattern to a
given problem is already an optimal solution.
Popular programming design patterns
● Gang of Four (GoF) design patterns
https://github.com/fbeline/design-patterns-JS
● S.O.L.I.D. principles
https://medium.com/@cramirez92/s-o-l-i-d-the-first-5-priciples-of-object-or
iented-design-with-javascript-790f6ac9b9fa
See more at: Functional programming design patterns by Scott Wlaschin
https://vimeo.com/113588389
Principles of Functional Programming (FP)
● Functions are "first class citizens"
● Immutable data structures
Functions composition
increase code readability
const increment = num => num + 5
const decrement = num => num - 3
const multiply = num => num * 2
const numbersOperation = num => increment(decrement(multiply(num)))
console.log(numbersOperation(15)) // 32
Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
Functions as parameters
increase code flexibility
const numbers = [1, 5, 8, 10, 21]
const plusOne = num => num + 1
console.log(numbers.map(plusOne)) // [2, 6, 9, 11, 22]
Higher-order functions
help to reuse code
const numbers = [1, 5, 8, 10, 21]
const createAddingFunction = number => arr =>
arr.map(num => num + number)
const numbersPlusOne = createAddingFunction(1)
console.log(numbersPlusOne(numbers)) // [2, 6, 9, 11, 22]
Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
Immutability
It’s not about forbidding change, but how to handle change.
Instead of modifying something you create something new
with your change applied.
Immutability
unintended objects mutation bug
const numbers = [1, 5, 8, 10, 21]
const numbersPlusOne = numbers => {
for(let i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i] + 1
}
return numbers
}
console.log(numbersPlusOne(numbers)) // [2, 6, 9, 11, 22] - the output we wanted
console.log(numbers) // [2, 6, 9, 11, 22] - the side effect we did not want!
Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
Immutability
create new data instead of changing data
const numbers = [1, 5, 8, 10, 21]
const createAddingFunction = number => arr => arr.map(num => num + number)
const numbersPlusOne = createAddingFunction(1)
console.log(numbersPlusOne(numbers)) // [2, 6, 9, 11, 22] - the output we wanted
console.log(numbers) // [1, 5, 8, 10, 21] - numbers are not changed by function!
Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
Design patterns in React
● Components
● Data-Down, Actions-Up
React Component Patterns by Michael Chan
https://www.youtube.com/watch?v=YaZg8wg39QQ
● Stateful component
● Stateless component
● Container component
● Higher-order component
● Render props
reactpatterns.com
● Function component
● Destructuring props
● JSX spread attributes
● Merge destructured props with other
values
● Conditional rendering
● Children types
● Array as children
● Function as children
● Render prop
● Children pass-through
● Proxy component
● Style component
● Event switch
● Layout component
● Container component
● Higher-order component
● State hoisting
● Controlled input
github.com/vasanthk/react-bits
● Conditional in JSX
● Async Nature Of setState()
● Dependency Injection
● Context Wrapper
● Event Handlers
● Flux Pattern
● One Way Data Flow
● Presentational vs Container
● Third Party Integration
● Passing Function To setState()
● Decorators
● Feature Flags
● Component Switch
● Reaching Into A Component
● List Components
● Format Text via Component
● Share Tracking Logic
● Toggle UI Elements
● HOC for Feature Toggles
● HOC props proxy
● Wrapper Components
● Display Order Variations
Data-Down, Actions-Up
Summary
Functional programming principles are the foundation of
design patterns in React.
Following FP and design patterns in React leads to optimal
solutions. Get to know them!

More Related Content

What's hot

Jetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIJetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIGilang Ramadhan
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentationritika1
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency InjectionNir Kaufman
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksMaulik Shah
 
Clean architecture
Clean architectureClean architecture
Clean architecture.NET Crowd
 
DevNetCreate Workshop - build a react app - React crash course
DevNetCreate Workshop - build a react app - React crash courseDevNetCreate Workshop - build a react app - React crash course
DevNetCreate Workshop - build a react app - React crash courseCisco DevNet
 
Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptxGDSCVJTI
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React NativeSambhu Lakshmanan
 
Abstract Factory Pattern
Abstract Factory PatternAbstract Factory Pattern
Abstract Factory Patternguestcb0002
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]GDSC UofT Mississauga
 

What's hot (20)

Jetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIJetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UI
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React/Redux
React/ReduxReact/Redux
React/Redux
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
DevNetCreate Workshop - build a react app - React crash course
DevNetCreate Workshop - build a react app - React crash courseDevNetCreate Workshop - build a react app - React crash course
DevNetCreate Workshop - build a react app - React crash course
 
Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptx
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
NestJS
NestJSNestJS
NestJS
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
React context
React context  React context
React context
 
Abstract Factory Pattern
Abstract Factory PatternAbstract Factory Pattern
Abstract Factory Pattern
 
React hooks
React hooksReact hooks
React hooks
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 

Similar to Design Patterns in React

London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010Skills Matter
 
CIS110 Computer Programming Design Chapter (1)
CIS110 Computer Programming Design Chapter  (1)CIS110 Computer Programming Design Chapter  (1)
CIS110 Computer Programming Design Chapter (1)Dr. Ahmed Al Zaidy
 
chapter1-161229182113 (1).pdf
chapter1-161229182113 (1).pdfchapter1-161229182113 (1).pdf
chapter1-161229182113 (1).pdfBernardVelasco1
 
Aspect Oriented Software Development
Aspect Oriented Software DevelopmentAspect Oriented Software Development
Aspect Oriented Software DevelopmentJignesh Patel
 
Sumit Gulwani at AI Frontiers : Programming by Examples
Sumit Gulwani at AI Frontiers : Programming by ExamplesSumit Gulwani at AI Frontiers : Programming by Examples
Sumit Gulwani at AI Frontiers : Programming by ExamplesAI Frontiers
 
Lecture 7 agile software development (2)
Lecture 7   agile software development (2)Lecture 7   agile software development (2)
Lecture 7 agile software development (2)IIUI
 
CIS 331 Technology levels--snaptutorial.com
CIS 331 Technology levels--snaptutorial.comCIS 331 Technology levels--snaptutorial.com
CIS 331 Technology levels--snaptutorial.comsholingarjosh56
 
Cis 331 Success Begins / snaptutorial.com
Cis 331 Success Begins / snaptutorial.comCis 331 Success Begins / snaptutorial.com
Cis 331 Success Begins / snaptutorial.comRobinson069
 
CIS 331 Education Redefined / snaptutorial.com
CIS 331  Education Redefined / snaptutorial.comCIS 331  Education Redefined / snaptutorial.com
CIS 331 Education Redefined / snaptutorial.comMcdonaldRyan200
 
Class[3][5th jun] [three js]
Class[3][5th jun] [three js]Class[3][5th jun] [three js]
Class[3][5th jun] [three js]Saajid Akram
 
Automatic Graphical Design Generator
Automatic Graphical Design GeneratorAutomatic Graphical Design Generator
Automatic Graphical Design GeneratorIRJET Journal
 
Paving the path towards platform engineering using a comprehensive reference...
Paving the path towards platform engineering  using a comprehensive reference...Paving the path towards platform engineering  using a comprehensive reference...
Paving the path towards platform engineering using a comprehensive reference...Kees C. Bakker
 

Similar to Design Patterns in React (20)

London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010London F-Sharp User Group : Don Syme on F# - 09/09/2010
London F-Sharp User Group : Don Syme on F# - 09/09/2010
 
CIS110 Computer Programming Design Chapter (1)
CIS110 Computer Programming Design Chapter  (1)CIS110 Computer Programming Design Chapter  (1)
CIS110 Computer Programming Design Chapter (1)
 
chapter1-161229182113 (1).pdf
chapter1-161229182113 (1).pdfchapter1-161229182113 (1).pdf
chapter1-161229182113 (1).pdf
 
Aspect Oriented Software Development
Aspect Oriented Software DevelopmentAspect Oriented Software Development
Aspect Oriented Software Development
 
Sumit Gulwani at AI Frontiers : Programming by Examples
Sumit Gulwani at AI Frontiers : Programming by ExamplesSumit Gulwani at AI Frontiers : Programming by Examples
Sumit Gulwani at AI Frontiers : Programming by Examples
 
MSR Asia Summit
MSR Asia SummitMSR Asia Summit
MSR Asia Summit
 
Build 2019 Recap
Build 2019 RecapBuild 2019 Recap
Build 2019 Recap
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
project_details
project_detailsproject_details
project_details
 
Cocomomodel
CocomomodelCocomomodel
Cocomomodel
 
COCOMO Model
COCOMO ModelCOCOMO Model
COCOMO Model
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Lecture 7 agile software development (2)
Lecture 7   agile software development (2)Lecture 7   agile software development (2)
Lecture 7 agile software development (2)
 
CIS 331 Technology levels--snaptutorial.com
CIS 331 Technology levels--snaptutorial.comCIS 331 Technology levels--snaptutorial.com
CIS 331 Technology levels--snaptutorial.com
 
Cis 331 Success Begins / snaptutorial.com
Cis 331 Success Begins / snaptutorial.comCis 331 Success Begins / snaptutorial.com
Cis 331 Success Begins / snaptutorial.com
 
CIS 331 Education Redefined / snaptutorial.com
CIS 331  Education Redefined / snaptutorial.comCIS 331  Education Redefined / snaptutorial.com
CIS 331 Education Redefined / snaptutorial.com
 
Class[3][5th jun] [three js]
Class[3][5th jun] [three js]Class[3][5th jun] [three js]
Class[3][5th jun] [three js]
 
Automatic Graphical Design Generator
Automatic Graphical Design GeneratorAutomatic Graphical Design Generator
Automatic Graphical Design Generator
 
Paving the path towards platform engineering using a comprehensive reference...
Paving the path towards platform engineering  using a comprehensive reference...Paving the path towards platform engineering  using a comprehensive reference...
Paving the path towards platform engineering using a comprehensive reference...
 
Modular programming
Modular programmingModular programming
Modular programming
 

More from Tomasz Bak

Building React CRUD app in minutes?
Building React CRUD app in minutes?Building React CRUD app in minutes?
Building React CRUD app in minutes?Tomasz Bak
 
How to migrate large project from Angular to React
How to migrate large project from Angular to ReactHow to migrate large project from Angular to React
How to migrate large project from Angular to ReactTomasz Bak
 
e2e testing with cypress
e2e testing with cypresse2e testing with cypress
e2e testing with cypressTomasz Bak
 
How to GraphQL: React Apollo
How to GraphQL: React ApolloHow to GraphQL: React Apollo
How to GraphQL: React ApolloTomasz Bak
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQLTomasz Bak
 
Working with npm packages
Working with npm packagesWorking with npm packages
Working with npm packagesTomasz Bak
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?Tomasz Bak
 
Functional Reactive Angular 2
Functional Reactive Angular 2 Functional Reactive Angular 2
Functional Reactive Angular 2 Tomasz Bak
 
Jak wnieść wkład w Open Source?
Jak wnieść wkład w Open Source?Jak wnieść wkład w Open Source?
Jak wnieść wkład w Open Source?Tomasz Bak
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript PromisesTomasz Bak
 
Replacing Rails asset pipeline with Gulp
Replacing Rails asset pipeline with GulpReplacing Rails asset pipeline with Gulp
Replacing Rails asset pipeline with GulpTomasz Bak
 
Ulepszanie aplikacji webowej z AngularJS
Ulepszanie aplikacji webowej z AngularJSUlepszanie aplikacji webowej z AngularJS
Ulepszanie aplikacji webowej z AngularJSTomasz Bak
 
Bardziej produktywny gmail
Bardziej produktywny gmailBardziej produktywny gmail
Bardziej produktywny gmailTomasz Bak
 
Rails tobak2005
Rails tobak2005Rails tobak2005
Rails tobak2005Tomasz Bak
 
Testowanie JavaScript
Testowanie JavaScriptTestowanie JavaScript
Testowanie JavaScriptTomasz Bak
 

More from Tomasz Bak (18)

Building React CRUD app in minutes?
Building React CRUD app in minutes?Building React CRUD app in minutes?
Building React CRUD app in minutes?
 
How to migrate large project from Angular to React
How to migrate large project from Angular to ReactHow to migrate large project from Angular to React
How to migrate large project from Angular to React
 
JAMstack
JAMstackJAMstack
JAMstack
 
e2e testing with cypress
e2e testing with cypresse2e testing with cypress
e2e testing with cypress
 
How to GraphQL: React Apollo
How to GraphQL: React ApolloHow to GraphQL: React Apollo
How to GraphQL: React Apollo
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQL
 
Working with npm packages
Working with npm packagesWorking with npm packages
Working with npm packages
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?
 
Functional Reactive Angular 2
Functional Reactive Angular 2 Functional Reactive Angular 2
Functional Reactive Angular 2
 
Jak wnieść wkład w Open Source?
Jak wnieść wkład w Open Source?Jak wnieść wkład w Open Source?
Jak wnieść wkład w Open Source?
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Replacing Rails asset pipeline with Gulp
Replacing Rails asset pipeline with GulpReplacing Rails asset pipeline with Gulp
Replacing Rails asset pipeline with Gulp
 
Ulepszanie aplikacji webowej z AngularJS
Ulepszanie aplikacji webowej z AngularJSUlepszanie aplikacji webowej z AngularJS
Ulepszanie aplikacji webowej z AngularJS
 
Bardziej produktywny gmail
Bardziej produktywny gmailBardziej produktywny gmail
Bardziej produktywny gmail
 
Kerberos
KerberosKerberos
Kerberos
 
Rails tobak2005
Rails tobak2005Rails tobak2005
Rails tobak2005
 
Ldap novell
Ldap novellLdap novell
Ldap novell
 
Testowanie JavaScript
Testowanie JavaScriptTestowanie JavaScript
Testowanie JavaScript
 

Recently uploaded

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Design Patterns in React

  • 1. Design Patterns in React 27 June, 2019 Tomasz Bąk tb@tomaszbak.com
  • 2.
  • 3. Agenda ● Why we need software design patterns? ● Principles of Functional Programming (FP) ● Design patterns in React
  • 4. What are software design patterns? ● a description or template for how to solve a problem ● they fit between the programming paradigm and a concrete algorithm Programming paradigm (i.e. OOP, functional) Design patterns (i.e. Container Pattern) Algorithm (i.e. your components)
  • 5. Why we need software design patterns? ● to understand the high-level design of the code ● to apply proven solutions to common problems It is often the case that you won’t need to refactor the code later on because applying the correct design pattern to a given problem is already an optimal solution.
  • 6. Popular programming design patterns ● Gang of Four (GoF) design patterns https://github.com/fbeline/design-patterns-JS ● S.O.L.I.D. principles https://medium.com/@cramirez92/s-o-l-i-d-the-first-5-priciples-of-object-or iented-design-with-javascript-790f6ac9b9fa
  • 7. See more at: Functional programming design patterns by Scott Wlaschin https://vimeo.com/113588389
  • 8. Principles of Functional Programming (FP) ● Functions are "first class citizens" ● Immutable data structures
  • 9. Functions composition increase code readability const increment = num => num + 5 const decrement = num => num - 3 const multiply = num => num * 2 const numbersOperation = num => increment(decrement(multiply(num))) console.log(numbersOperation(15)) // 32 Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
  • 10. Functions as parameters increase code flexibility const numbers = [1, 5, 8, 10, 21] const plusOne = num => num + 1 console.log(numbers.map(plusOne)) // [2, 6, 9, 11, 22]
  • 11. Higher-order functions help to reuse code const numbers = [1, 5, 8, 10, 21] const createAddingFunction = number => arr => arr.map(num => num + number) const numbersPlusOne = createAddingFunction(1) console.log(numbersPlusOne(numbers)) // [2, 6, 9, 11, 22] Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
  • 12. Immutability It’s not about forbidding change, but how to handle change. Instead of modifying something you create something new with your change applied.
  • 13. Immutability unintended objects mutation bug const numbers = [1, 5, 8, 10, 21] const numbersPlusOne = numbers => { for(let i = 0; i < numbers.length; i++) { numbers[i] = numbers[i] + 1 } return numbers } console.log(numbersPlusOne(numbers)) // [2, 6, 9, 11, 22] - the output we wanted console.log(numbers) // [2, 6, 9, 11, 22] - the side effect we did not want! Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
  • 14. Immutability create new data instead of changing data const numbers = [1, 5, 8, 10, 21] const createAddingFunction = number => arr => arr.map(num => num + number) const numbersPlusOne = createAddingFunction(1) console.log(numbersPlusOne(numbers)) // [2, 6, 9, 11, 22] - the output we wanted console.log(numbers) // [1, 5, 8, 10, 21] - numbers are not changed by function! Source: https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
  • 15. Design patterns in React ● Components ● Data-Down, Actions-Up
  • 16. React Component Patterns by Michael Chan https://www.youtube.com/watch?v=YaZg8wg39QQ ● Stateful component ● Stateless component ● Container component ● Higher-order component ● Render props
  • 17. reactpatterns.com ● Function component ● Destructuring props ● JSX spread attributes ● Merge destructured props with other values ● Conditional rendering ● Children types ● Array as children ● Function as children ● Render prop ● Children pass-through ● Proxy component ● Style component ● Event switch ● Layout component ● Container component ● Higher-order component ● State hoisting ● Controlled input
  • 18. github.com/vasanthk/react-bits ● Conditional in JSX ● Async Nature Of setState() ● Dependency Injection ● Context Wrapper ● Event Handlers ● Flux Pattern ● One Way Data Flow ● Presentational vs Container ● Third Party Integration ● Passing Function To setState() ● Decorators ● Feature Flags ● Component Switch ● Reaching Into A Component ● List Components ● Format Text via Component ● Share Tracking Logic ● Toggle UI Elements ● HOC for Feature Toggles ● HOC props proxy ● Wrapper Components ● Display Order Variations
  • 20. Summary Functional programming principles are the foundation of design patterns in React. Following FP and design patterns in React leads to optimal solutions. Get to know them!