The document discusses the integration of functional programming principles with ReactJS, emphasizing concepts like immutability, pure functions, and higher-order components. It highlights the advantages of using stateless components, avoiding shared state, and implementing memoization for performance optimization. Lastly, it provides code examples and resources for further exploration of functional programming 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
3.
Functional Programming Functional programmingis 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.
Function & ReactJSfunctional 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-Orderterms 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 ReactJSComponent 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?
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/
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.