SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
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!

Contenu connexe

Tendances

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)paramisoft
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsDinesh U
 
Branching Your Way to Low-Code Perfection
Branching Your Way to Low-Code PerfectionBranching Your Way to Low-Code Perfection
Branching Your Way to Low-Code PerfectionOutSystems
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
Clean architecture
Clean architectureClean architecture
Clean architectureLieven Doclo
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMJimit Shah
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern11prasoon
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Prashanth Kumar
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.jsDoug Neiner
 
Clean code in JavaScript
Clean code in JavaScriptClean code in JavaScript
Clean code in JavaScriptMathieu Breton
 
Kata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and AdaptersKata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and Adaptersholsky
 
Clean architecture
Clean architectureClean architecture
Clean architecture.NET Crowd
 
Introduction to React Native - Nader Dabit
Introduction to React Native - Nader DabitIntroduction to React Native - Nader Dabit
Introduction to React Native - Nader DabitAmazon Web Services
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.boyney123
 

Tendances (20)

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Branching Your Way to Low-Code Perfection
Branching Your Way to Low-Code PerfectionBranching Your Way to Low-Code Perfection
Branching Your Way to Low-Code Perfection
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 
React native
React nativeReact native
React native
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
 
Clean code in JavaScript
Clean code in JavaScriptClean code in JavaScript
Clean code in JavaScript
 
Kata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and AdaptersKata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and Adapters
 
React Workshop
React WorkshopReact Workshop
React Workshop
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Template pattern
Template patternTemplate pattern
Template pattern
 
Introduction to React Native - Nader Dabit
Introduction to React Native - Nader DabitIntroduction to React Native - Nader Dabit
Introduction to React Native - Nader Dabit
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus 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
 

Dernier

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)wesley chun
 
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 educationjfdjdjcjdnsjd
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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.pdfsudhanshuwaghmare1
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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 Takeoffsammart93
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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 Scriptwesley chun
 

Dernier (20)

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)
 
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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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
 

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!