SlideShare a Scribd company logo
ReactJS &
Functional
Programming
principles
Andrii Lundiak @ GlobalLogic
Facebook: Andrii Lundiak
Twitter: @landike
GitHub: @alundiak
Touch: A bit of theory and
code examples
in JavaScript and ReactJS
Agenda
● Immutability & ReactJS
● JS Function vs. ReactJS Functional Component & ReactJS Hooks
● First-Class & High-Order terms
● Memoizing
TODO:
● Lambda Calculus
● Avoid shared state
● Avoid side effects
● Strict/non-strict evaluation
Functional Programming
Functional programming is the process of building software by composing pure functions,
avoiding shared state, mutable data, and side-effects.
“In computer science, functional programming is a programming paradigm that treats computation as
the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the
application of functions, in contrast to the imperative programming style, which emphasizes changes in
state. Functional programming has its roots in the lambda calculus, a formal system developed in the
1930s to investigate function definition, function application, and recursion.” Wiki.
Stateless vs. Stateful
https://medium.com/@DarkMordor/common-code-mistakes-in-react-that-you-maybe-made-18acce2787bf
“(react/prefer-stateless-function)
It’s more improvement in code and app than an error, but I recommend you to follow this rule. If
your component doesn’t use state than make it the stateless component”
https://medium.com/@DarkMordor/common-code-mistakes-in-react-that-you-maybe-made-1
8acce2787bf
“(react/no-direct-mutation-state)
State mutation is a huge error. Uncontrollable state mutation will lead to untraceable bugs and
big problems. ”
// When it is “broken” immutability?
Immutability & ReactJS this.props
Function & ReactJS functional component
// JavaScript
function add(a, b) { return a + b; }
// ReactJS
export function FunctionalAddComponent(props) { // aka stateless function
const { a, b } = props;
return ( <div>{a + b}</div> );
}
// When it is “broken” functional component?
Pure function vs. Pure component
● “If your React component’s render() function renders the same result given the same props and
state, you can use React.PureComponent for a performance boost in some cases.”
● “Hooks let you use more of React’s features without classes.Conceptually, React components
have always been closer to functions. Hooks embrace functions, but without sacrificing the
practical spirit of React. ”. Details.
First-Class & High-Order terms
1. In Math: High-Order function
2. In Programming: First-class function , MDN details.
● “First-class functions - functions which can be as arguments to other functions, returning them as the
values from other functions, and assigning them to variables or storing them in data structures”
● “First-class functions are a necessity for the functional programming style, in which the use of higher-order
functions is a standard practice.”
● JS: first-class/higher-order functions : filter(), map() and reduce(). Details.
● “We can also look at closures as first-class functions with bound variables. ”. Details.
● “setTimeout is of arity 2, or equivalently say that is a binary function”. Details.
High Order ReactJS Component
JavaScript:
// A Higher-Order Function is a FUNCTION that takes another FUNCTION as an input, returns
a FUNCTION or does both.
ReactJS:
// A Higher-Order Component is a FUNCTION that takes a COMPONENT and returns a new
COMPONENT.
“HOC doesn’t modify the input component, nor does it use inheritance to copy its behavior. Rather, a HOC composes the original
component by wrapping it in a container component. A HOC is a pure function with zero side-effects.”. Details. Users/Stocks
ReactJS example.
// When it is “broken” HOC component?
hof() and hoc()
// JavaScript
function hof() {
const firstName = 'Andrii';
return function (lastName) {
return `${firstName} ${lastName}`;
};
}
// ReactJS
const createHOC = (WrappedComponent, data) => {
class HocClass extends React.Component {
render() {
return <div> <WrappedComponent {...data} /> </div>;
}
}
return HocClass;
};
Currying, Derivative, Calculus
● Currying. “In mathematics and computer science, currying is the technique of translating the
evaluation of a function that takes multiple arguments into evaluating a sequence of functions,
each with a single argument. ”
● Derivative. “In other words, every value of x chooses a function, denoted fx
, which is a function of
one real number”. x => f(x) . Also related to Calculus.
● Function Composition - f( g( h(x) ) )
● TODO: Lambda Calculus
Currying in JavaScript
// JavaScript
const notCurry = (x, y, z) => x + y + z; // a regular function
const curry = x => y => z => x + y + z; // a curry function
// ReactJS
const reverse = PassedComponent => ({ children, ...props }) => (
<PassedComponent {...props}>
{children.split("").reverse().join("")}
</PassedComponent>
);
// Redux
export const withMiddleware = store => next => action => {
// do something, next(action) or state.dispatch();
}
Memoization
● “Memoization - storing the results of expensive function calls and returning the cached result
when the same inputs occur again.”
● “React memo() - s a higher order component. It’s similar to React.PureComponent but for
function components instead of classes. If your function component renders the same result
given the same props, you can wrap it in a call to React.memo for a performance boost in some
cases by memoizing the result. This means that React will skip rendering the component, and
reuse the last rendered result".
●
Good expl
https://logrocket.com/blog/pure-functional-components/
// Approach 1
export const MyMemoComponentWithFuncComp = React.memo(FunctionalComponent );
// Approach 2.1
export const MyMemoComponentWithRegularFunc = React.memo(function FunctionalComponent (props) {
const { msg } = props;
return <div> FunctionalComponent says: {msg}. </div>;
});
// Approach 2.2
export const MyMemoComponentWithFatArrow = React.memo((props) => {
const { msg } = props;
return <div> FunctionalComponent says: {msg}.</div>;
});
React.memo() is Functional HOC
Demo time
ReactJS tech/code outcomes
● ReactJS combines many things, is very flexible and allows you to choose, what suits you
best.
● Don’t mutate this.props from components inside
● Don’t mutate this.state this.state directly.
● It matters for debugging how you name your function, component, wrapping and wrapped
components.
Github repos
● https://github.com/stoeffel/awesome-fp-js
● https://github.com/markerikson/react-redux-links/blob/master/functional-programming.md
● https://github.com/alundiak/fp-examples
Read: FP in JavaScript
● https://en.wikipedia.org/wiki/Functional_programming#JavaScript
● https://dev.to/leandrotk_/functional-programming-principles-in-javascript-26g7
● https://medium.com/dailyjs/tagged/functional-programming
● https://medium.com/javascript-scene/tagged/functional-programming
● https://medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a
0
● https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536
● https://medium.freecodecamp.org/discover-the-power-of-first-class-functions-fd0d7b599b69
● https://medium.freecodecamp.org/an-introduction-to-functional-programming-style-in-javascript-71fcc050f064
● https://hackernoon.com/javascript-and-functional-programming-pt-2-first-class-functions-4437a1aec217
● https://medium.com/front-end-weekly/javascript-es6-curry-functions-with-practical-examples-6ba2ced003b1
Read: FP in ReactJS
● https://lispcast.com/is-react-functional-programming/
● https://medium.com/@agm1984/an-overview-of-functional-programming-in-javascript-and-react-part-on
e-10d75b509e9e
● https://levelup.gitconnected.com/functional-react-is-it-possible-ceaf5ed91bfd
● https://www.dropsource.com/blog/functional-programming-principles-in-react-and-flux/
● https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/
● https://medium.com/@andrea.chiarelli/the-functional-side-of-react-229bdb26d9a6
● https://hackernoon.com/curry-away-in-react-7c4ed110c65a
Video (PL):
https://www.youtube.com/watch?v=8rUKMWiT5Y4
Q&A
Facebook: Andrii Lundiak
Twitter: @landike
GitHub: @alundiak

More Related Content

What's hot

React js for beginners
React js for beginnersReact js for beginners
React js for beginners
Alessandro Valenti
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
Ignacio Martín
 
Reactjs
Reactjs Reactjs
Reactjs
Neha Sharma
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
Imran Sayed
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to React
Rob Quick
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS
Hamed Farag
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
namespaceit
 
ReactJs
ReactJsReactJs
React workshop
React workshopReact workshop
React workshop
Imran Sayed
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Samundra khatri
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
Ritesh Mehrotra
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
Piyush Jamwal
 
React js
React jsReact js
React js
Rajesh Kolla
 
React and redux
React and reduxReact and redux
React and redux
Mystic Coders, LLC
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022
Fabio Biondi
 
Intro to React
Intro to ReactIntro to React
Intro to React
Eric Westfall
 
React state
React  stateReact  state
React state
Ducat
 
React hooks
React hooksReact hooks
React hooks
Assaf Gannon
 
React web development
React web developmentReact web development
React web development
Rully Ramanda
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_js
MicroPyramid .
 

What's hot (20)

React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Reactjs
Reactjs Reactjs
Reactjs
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to React
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 
ReactJs
ReactJsReactJs
ReactJs
 
React workshop
React workshopReact workshop
React workshop
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
React js
React jsReact js
React js
 
React and redux
React and reduxReact and redux
React and redux
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
React state
React  stateReact  state
React state
 
React hooks
React hooksReact hooks
React hooks
 
React web development
React web developmentReact web development
React web development
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_js
 

Similar to React JS & Functional Programming Principles

Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
ReactJS
ReactJSReactJS
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
Remo Jansen
 
Intro to React.js
Intro to React.jsIntro to React.js
Intro to React.js
Smita Prasad
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
Jeffrey Sanchez
 
A React Journey
A React JourneyA React Journey
A React Journey
LinkMe Srl
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR3
 
react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
DayNightGaMiNg
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
Nitin Tyagi
 
react-slides.pdf
react-slides.pdfreact-slides.pdf
react-slides.pdf
DayNightGaMiNg
 
react-slides.pdf gives information about react library
react-slides.pdf gives information about react libraryreact-slides.pdf gives information about react library
react-slides.pdf gives information about react library
janet736113
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
Codemotion
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
StephieJohn
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJS
Pratik Majumdar
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
DivyanshGupta922023
 
React
ReactReact
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
Walking Tree Technologies
 
Predictable reactive state management - ngrx
Predictable reactive state management - ngrxPredictable reactive state management - ngrx
Predictable reactive state management - ngrx
Ilia Idakiev
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...
Katy Slemon
 

Similar to React JS & Functional Programming Principles (20)

Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
ReactJS
ReactJSReactJS
ReactJS
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Intro to React.js
Intro to React.jsIntro to React.js
Intro to React.js
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
 
ReactJS (1)
ReactJS (1)ReactJS (1)
ReactJS (1)
 
A React Journey
A React JourneyA React Journey
A React Journey
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
 
react-slides.pdf
react-slides.pdfreact-slides.pdf
react-slides.pdf
 
react-slides.pdf gives information about react library
react-slides.pdf gives information about react libraryreact-slides.pdf gives information about react library
react-slides.pdf gives information about react library
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJS
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
React
ReactReact
React
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Predictable reactive state management - ngrx
Predictable reactive state management - ngrxPredictable reactive state management - ngrx
Predictable reactive state management - ngrx
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...
 

More from Andrii Lundiak

Create ReactJS Component & publish as npm package
Create ReactJS Component & publish as npm packageCreate ReactJS Component & publish as npm package
Create ReactJS Component & publish as npm package
Andrii Lundiak
 
Node js packages [#howto with npm]
Node js packages [#howto with npm]Node js packages [#howto with npm]
Node js packages [#howto with npm]
Andrii Lundiak
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]
Andrii Lundiak
 
Grunt Delicious
Grunt DeliciousGrunt Delicious
Grunt Delicious
Andrii Lundiak
 
Mockups & Requirements [ITdeya @ IF_IT_S]
Mockups & Requirements [ITdeya @ IF_IT_S]Mockups & Requirements [ITdeya @ IF_IT_S]
Mockups & Requirements [ITdeya @ IF_IT_S]
Andrii Lundiak
 
Drupal Vs Other
Drupal Vs OtherDrupal Vs Other
Drupal Vs Other
Andrii Lundiak
 
Drupal Deployment Troubles and Problems
Drupal Deployment Troubles and ProblemsDrupal Deployment Troubles and Problems
Drupal Deployment Troubles and Problems
Andrii Lundiak
 
Election
ElectionElection
Election
Andrii Lundiak
 

More from Andrii Lundiak (8)

Create ReactJS Component & publish as npm package
Create ReactJS Component & publish as npm packageCreate ReactJS Component & publish as npm package
Create ReactJS Component & publish as npm package
 
Node js packages [#howto with npm]
Node js packages [#howto with npm]Node js packages [#howto with npm]
Node js packages [#howto with npm]
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]
 
Grunt Delicious
Grunt DeliciousGrunt Delicious
Grunt Delicious
 
Mockups & Requirements [ITdeya @ IF_IT_S]
Mockups & Requirements [ITdeya @ IF_IT_S]Mockups & Requirements [ITdeya @ IF_IT_S]
Mockups & Requirements [ITdeya @ IF_IT_S]
 
Drupal Vs Other
Drupal Vs OtherDrupal Vs Other
Drupal Vs Other
 
Drupal Deployment Troubles and Problems
Drupal Deployment Troubles and ProblemsDrupal Deployment Troubles and Problems
Drupal Deployment Troubles and Problems
 
Election
ElectionElection
Election
 

Recently uploaded

test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
Output determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CCOutput determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CC
ShahulHameed54211
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
Himani415946
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptxLiving-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
TristanJasperRamos
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 

Recently uploaded (16)

test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
Output determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CCOutput determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CC
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptxLiving-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 

React JS & Functional Programming Principles

  • 1. ReactJS & Functional Programming principles Andrii Lundiak @ GlobalLogic Facebook: Andrii Lundiak Twitter: @landike GitHub: @alundiak Touch: A bit of theory and code examples in JavaScript and ReactJS
  • 2. Agenda ● Immutability & ReactJS ● JS Function vs. ReactJS Functional Component & ReactJS Hooks ● First-Class & High-Order terms ● Memoizing TODO: ● Lambda Calculus ● Avoid shared state ● Avoid side effects ● Strict/non-strict evaluation
  • 3. Functional Programming Functional programming is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. “In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion.” Wiki.
  • 4. Stateless vs. Stateful https://medium.com/@DarkMordor/common-code-mistakes-in-react-that-you-maybe-made-18acce2787bf “(react/prefer-stateless-function) It’s more improvement in code and app than an error, but I recommend you to follow this rule. If your component doesn’t use state than make it the stateless component”
  • 5. https://medium.com/@DarkMordor/common-code-mistakes-in-react-that-you-maybe-made-1 8acce2787bf “(react/no-direct-mutation-state) State mutation is a huge error. Uncontrollable state mutation will lead to untraceable bugs and big problems. ” // When it is “broken” immutability? Immutability & ReactJS this.props
  • 6. Function & ReactJS functional component // JavaScript function add(a, b) { return a + b; } // ReactJS export function FunctionalAddComponent(props) { // aka stateless function const { a, b } = props; return ( <div>{a + b}</div> ); } // When it is “broken” functional component?
  • 7. Pure function vs. Pure component ● “If your React component’s render() function renders the same result given the same props and state, you can use React.PureComponent for a performance boost in some cases.” ● “Hooks let you use more of React’s features without classes.Conceptually, React components have always been closer to functions. Hooks embrace functions, but without sacrificing the practical spirit of React. ”. Details.
  • 8. First-Class & High-Order terms 1. In Math: High-Order function 2. In Programming: First-class function , MDN details. ● “First-class functions - functions which can be as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures” ● “First-class functions are a necessity for the functional programming style, in which the use of higher-order functions is a standard practice.” ● JS: first-class/higher-order functions : filter(), map() and reduce(). Details. ● “We can also look at closures as first-class functions with bound variables. ”. Details. ● “setTimeout is of arity 2, or equivalently say that is a binary function”. Details.
  • 9. High Order ReactJS Component JavaScript: // A Higher-Order Function is a FUNCTION that takes another FUNCTION as an input, returns a FUNCTION or does both. ReactJS: // A Higher-Order Component is a FUNCTION that takes a COMPONENT and returns a new COMPONENT. “HOC doesn’t modify the input component, nor does it use inheritance to copy its behavior. Rather, a HOC composes the original component by wrapping it in a container component. A HOC is a pure function with zero side-effects.”. Details. Users/Stocks ReactJS example. // When it is “broken” HOC component?
  • 10. hof() and hoc() // JavaScript function hof() { const firstName = 'Andrii'; return function (lastName) { return `${firstName} ${lastName}`; }; } // ReactJS const createHOC = (WrappedComponent, data) => { class HocClass extends React.Component { render() { return <div> <WrappedComponent {...data} /> </div>; } } return HocClass; };
  • 11. Currying, Derivative, Calculus ● Currying. “In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. ” ● Derivative. “In other words, every value of x chooses a function, denoted fx , which is a function of one real number”. x => f(x) . Also related to Calculus. ● Function Composition - f( g( h(x) ) ) ● TODO: Lambda Calculus
  • 12. Currying in JavaScript // JavaScript const notCurry = (x, y, z) => x + y + z; // a regular function const curry = x => y => z => x + y + z; // a curry function // ReactJS const reverse = PassedComponent => ({ children, ...props }) => ( <PassedComponent {...props}> {children.split("").reverse().join("")} </PassedComponent> ); // Redux export const withMiddleware = store => next => action => { // do something, next(action) or state.dispatch(); }
  • 13. Memoization ● “Memoization - storing the results of expensive function calls and returning the cached result when the same inputs occur again.” ● “React memo() - s a higher order component. It’s similar to React.PureComponent but for function components instead of classes. If your function component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result". ● Good expl https://logrocket.com/blog/pure-functional-components/
  • 14. // Approach 1 export const MyMemoComponentWithFuncComp = React.memo(FunctionalComponent ); // Approach 2.1 export const MyMemoComponentWithRegularFunc = React.memo(function FunctionalComponent (props) { const { msg } = props; return <div> FunctionalComponent says: {msg}. </div>; }); // Approach 2.2 export const MyMemoComponentWithFatArrow = React.memo((props) => { const { msg } = props; return <div> FunctionalComponent says: {msg}.</div>; }); React.memo() is Functional HOC
  • 16. ReactJS tech/code outcomes ● ReactJS combines many things, is very flexible and allows you to choose, what suits you best. ● Don’t mutate this.props from components inside ● Don’t mutate this.state this.state directly. ● It matters for debugging how you name your function, component, wrapping and wrapped components.
  • 17. Github repos ● https://github.com/stoeffel/awesome-fp-js ● https://github.com/markerikson/react-redux-links/blob/master/functional-programming.md ● https://github.com/alundiak/fp-examples
  • 18. Read: FP in JavaScript ● https://en.wikipedia.org/wiki/Functional_programming#JavaScript ● https://dev.to/leandrotk_/functional-programming-principles-in-javascript-26g7 ● https://medium.com/dailyjs/tagged/functional-programming ● https://medium.com/javascript-scene/tagged/functional-programming ● https://medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a 0 ● https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536 ● https://medium.freecodecamp.org/discover-the-power-of-first-class-functions-fd0d7b599b69 ● https://medium.freecodecamp.org/an-introduction-to-functional-programming-style-in-javascript-71fcc050f064 ● https://hackernoon.com/javascript-and-functional-programming-pt-2-first-class-functions-4437a1aec217 ● https://medium.com/front-end-weekly/javascript-es6-curry-functions-with-practical-examples-6ba2ced003b1
  • 19. Read: FP in ReactJS ● https://lispcast.com/is-react-functional-programming/ ● https://medium.com/@agm1984/an-overview-of-functional-programming-in-javascript-and-react-part-on e-10d75b509e9e ● https://levelup.gitconnected.com/functional-react-is-it-possible-ceaf5ed91bfd ● https://www.dropsource.com/blog/functional-programming-principles-in-react-and-flux/ ● https://codinglawyer.net/index.php/2018/01/31/taste-the-principles-of-functional-programming-in-react/ ● https://medium.com/@andrea.chiarelli/the-functional-side-of-react-229bdb26d9a6 ● https://hackernoon.com/curry-away-in-react-7c4ed110c65a Video (PL): https://www.youtube.com/watch?v=8rUKMWiT5Y4
  • 20. Q&A Facebook: Andrii Lundiak Twitter: @landike GitHub: @alundiak