SlideShare una empresa de Scribd logo
1 de 20
Descargar para leer sin conexión
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!

Más contenido relacionado

La actualidad más candente

Introduction To Single Page Application
Introduction To Single Page ApplicationIntroduction To Single Page Application
Introduction To Single Page Application
KMS Technology
 

La actualidad más candente (20)

Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
React workshop
React workshopReact workshop
React workshop
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Introduction To Single Page Application
Introduction To Single Page ApplicationIntroduction To Single Page Application
Introduction To Single Page Application
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
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
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React?
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
 
Javascript
JavascriptJavascript
Javascript
 
State management in react applications (Statecharts)
State management in react applications (Statecharts)State management in react applications (Statecharts)
State management in react applications (Statecharts)
 
React Router: React Meetup XXL
React Router: React Meetup XXLReact Router: React Meetup XXL
React Router: React Meetup XXL
 
Front end development best practices
Front end development best practicesFront end development best practices
Front end development best practices
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 

Similar a 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/2010
Skills Matter
 
Aspect Oriented Software Development
Aspect Oriented Software DevelopmentAspect Oriented Software Development
Aspect Oriented Software Development
Jignesh Patel
 

Similar a 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
 

Más de Tomasz Bak

Rails tobak2005
Rails tobak2005Rails tobak2005
Rails tobak2005
Tomasz Bak
 

Más de 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
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

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!