All posts by admin

JSX to tree DOM

The idea is that Then JSX gets transpiled to Plain Javascript Objects using React.createElement().
Then it compiles those objects down to DOM elements.

React Component Life Cycle

There are four different phases of React component’s lifecycle:

Initialization: In this phase react component prepares setting up the initial state and default props.

Mounting: The react component is ready to mount in the browser DOM. This phase covers componentWillMount and componentDidMount lifecycle methods.

Updating: In this phase, the component get updated in two ways, sending the new props and updating the state. This phase covers shouldComponentUpdate, componentWillUpdate and componentDidUpdate lifecycle methods.

Unmounting: In this last phase, the component is not needed and get unmounted from the browser DOM. This phase include componentWillUnmount lifecycle method.

Higher Order Component (HOC)

ref – https://reactjs.org/docs/higher-order-components.html
https://www.codingame.com/playgrounds/8595/reactjs-higher-order-components-tutorial

Whereas a component transforms props into UI, a higher-order component transforms a component into another component.

A 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 (does not modify outside values, unique input/output) with zero side-effects.

And that’s it! The wrapped component receives all the props of the container, along with a new prop, data, which it uses to render its output. The HOC isn’t concerned with how or why the data is used, and the wrapped component isn’t concerned with where the data came from.

Let’s look at an example. Create a simple React app like so.


npm install -g create-react-app
create-react-app my-app

cd my-app
npm start

It’s only a Higher Order component by definition because it does not modify outside values. It simply take another component as input and renders it.

Let’s create a generic utility component that displays data in a row. It will be used by two other components.

TableRow.js

StockList.js

Now we use that StockList in our App.

Everything is standard. We display a component inside of another component. But let’s say we want to add UserList, which is very very similar to StockList.

It even uses the TableRow component in the same way. Can we somehow refactor these two components into one?

This is where HOC comes into play. We can implement reusability of particular components in multiple modules or components.

Take a look at how we’d factor this. Let’s create an HOC file.

HOC.js

In our HOC, we receive the two components. However, we combine the functionality of passing data to them. Instead of them having its own data state.
Hence, we receive data from the parameter and use prop to pass it into those Wrapped components.

In our redundant components, our this.prop will have the data we need, instead of us keeping the data state in our component.
Thus, we use it right away in our render functions.

StockList.js

In both UserList and StockList, we are doing the same thing.
Just display the stocks and users properties id and name.
So here our component gets passed into our HOC, which then fills our
this.prop with the new data.

We then can display it.

UserList.js

We pull the data state out. Then pass both data and components into HOC, which creates two different components.

App.js

Because Hoc is a normal function, you can add as many or as few arguments as you like. For example, you could accept an argument that configures shouldComponentUpdate, or one that configures the data source. These are all possible because the HOC has full control over how the component is defined.

Like components, the contract between Hoc and the wrapped component is entirely props-based. This makes it easy to swap one HOC for a different one, as long as they provide the same props to the wrapped component. This may be useful if you change data-fetching libraries, for example.

Don’t Mutate the Original Component. Use Composition.

Resist the temptation to modify a component’s prototype (or otherwise mutate it) inside a HOC.

– The input component cannot be reused separately from the enhanced component because it depends on the original functionality of the input component. If you change it in your HOC, then your enhanced component will function differently from the original component.

– If you apply another HOC to EnhancedComponent that also mutates componentDidUpdate, the first HOC’s functionality will be overridden!

– Mutating HOCs are a leaky abstraction—the consumer must know how they are implemented in order to avoid conflicts with other HOCs.

Instead of mutation, HOCs should use composition, by wrapping the input component in a container component. In our example, we create a class component, then implement our componentDidUpdate the way we want, and then wrap our component inside the render via composition.

This HOC has the same functionality as the mutating version while avoiding the potential for clashes.
– It works equally well with class and function components.
– Because it’s a pure function, it’s composable with other HOCs, or even with itself.

Container components are part of a strategy of separating responsibility between high-level and low-level concerns. Containers manage things like subscriptions and state, and pass props to components that handle things like rendering UI. HOCs use containers as part of their implementation. You can think of HOCs as parameterized container component definitions.

Don’t Use HOCs Inside the render Method

React’s diffing algorithm (called reconciliation) uses component identity to determine whether it should update the existing subtree or throw it away and mount a new one.

When its analyze the previous and current render, it looks at the previous and current component. If the two components are identical (===), React will leave the subtree be, and continue to update the subtree by diffing it with the new one. If they’re not equal, the previous subtree is unmounted completely.
This means you can’t apply a HOC to a component within the render method of a component because for every render, a new component (object) returned. Hence every compare will be different and thus, the entire subtree will be remount each time.

Remounting a component causes the state of that component and all of its children to be lost also.

Instead, apply HOCs outside the component definition so that the resulting component is created only once. Then, its identity will be consistent across renders. This is usually what how you use it.

In those very rare cases where you need to apply a HOC dynamically, you can also do it inside a component’s lifecycle methods or its constructor.

Forward Ref (React)

ref – https://reactjs.org/docs/forwarding-refs.html

Here is a step-by-step explanation of what happens in the example:

Functional vs Class component (React)

ref – https://medium.com/@Zwenza/functional-vs-class-components-in-react-231e3fbd7108

Functional Component

A functional component is just a plain JavaScript function which:
– accepts props as an argument
– returns a React element.
– DOES NOT have state. Thus, you cannot use setState()
– No lifecycle methods

If you NEED a state in your component you will either need to:

1) create a class component or
2) you lift the state up to the parent component and pass it down the functional component via props.

So…. why should I use functional components at all?

– Functional component are much easier to read and test because they are plain JavaScript functions without state or lifecycle-hooks
– less code
– easier to separate container and presentation components.
– When we write presentation component that does not have its own state, or need lifecycle hooks.

Class Component

A class component requires you to extend from React.Component and create a render function which returns a React element. By extending from React.Component, you can over-ride lifecycle methods.

Passing props to super

ref – https://stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-in-react-when-using-e

There is only one reason when one needs to pass props to super(): When you want to access this.props in constructor.

Passing:

Not passing:

Note that passing or not passing props to super has no effect on later uses of this.props outside constructor.

That is render, shouldComponentUpdate, or event handlers always have access to it.

Controlled Component

ref – https://stackoverflow.com/questions/42522515/what-are-react-controlled-components-and-uncontrolled-components
https://reactjs.org/docs/forms.html#controlled-components

A Controlled Component is one that takes its current value through props and notifies changes through callbacks like onChange. A parent component “controls” it by handling the callback and updating its state. It then passes the updated back to the controlled component. You could also call this a “dumb component”.

The parent component’s hold on that single source of data is called “single source of truth”.

In regards to Elements

1) Since the value attribute is set to display component’s state, the displayed value will always be this.state.value. This makes React state the single source of truth.
2) Since handleChange runs on every keystroke to update the React state, the displayed value will update as the user types.
3) With a controlled component, the input’s value is always driven by the React state.

With a Textarea, we write it like this:

In regards to Components

We have controlled components ListGroup and Pagination. Notice we pass data into them to be processed and displayed. They then use callbacks such as selectedGenre, handlePageChange, handleNoGenre, handleGenreSelect to update parent component Movies’s state.

Pure components in React

ref – https://medium.com/technofunnel/working-with-react-pure-components-166ded26ae48
https://en.wikipedia.org/wiki/Pure_function
https://stackoverflow.com/questions/52680508/can-pure-functions-be-asynchronous
demo

Pure Functions

First, let’s talk about pure functions.

The definition of Pure Function says that for specific input parameters, we always have a specific output. The output is solely dependent on Input parameters and no other external variable.

Wikipedia defines it as:

  • Its return value is the same for the same arguments. 1 to 1. For a particular input, you won’t get different outputs. No mutation of local static variables, non-local variables, mutable reference arguments or I/O streams
  • Its evaluation has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams).

example:

It uses non-local variable initialValue. So it is impure.

The code specified above is another example of the impure function, where the function is modifying the global variable getInitialValue, which does not lie under the scope of this function. Modifying this variable results in the introduction of Impurity to the function, making it Impure.

The function given above defines the output on the basis of the input parameter only.
No outer variables is adding its impact to the output.
Also, we can see that the function does not modify any other variable that does not belong to this function. Hence it adheres to the concept of Pure Functions.

Browser optimizations via Pure Functions

Pure Functions have a huge performance benefit during execution on the Browser.

Picture a scenario where a specific Pure Function is getting called multiple times.
The Application calls for the same function multiple times with the same parameters — assume “add(10, 20)”.
After executing it multiple times, the Chrome V8 Engine tries to optimize the code further by storing the execution result of the following function call. On the next call to the same function, with the same parameter, instead of executing the function again, the cached result is returned. Hence enhancing the Application Performance.

Functions should not introduce side effects

If the application updates certain data that is observable outside the called function, it can be considered a side effect introduced by the function.

Here are a few scenarios:

  • Modifying any external variable or object property (DOM)
  • – self explanatory. You use or modify outside global variables such as the document object.

  • Logging data to the console
  • – Similar to DOM, it is using outside object (console). Also, it prints something onto the screen.

  • Writing Data to a file
  • – It changes data to a file, which is global.

  • Writing data to the network
  • – Modifies network resources, which is global.

  • Triggering any external process
  • Calling any other (impure) functions with side-effects
  • Making Asynchronous Data Calls
  • – asynchronous actions usually is impure.

    Most things that we use asynchronous functions for are inherently side-effect(ful): I/O, network stuff, timers. But even if we ignore those, promises alone rely on some kind of global state for their asynchrony: the event loop. This violates purity.

    Pure Components

    Pure Components in React are components which do not re-renders when the value of state and props has been updated with the same values.
    If the value of the previous state or props and the new state or props is the same, the component is not re-rendered. Pure Components restricts the re-rendering ensuring the higher performance of the Component.

    Features of React Pure Components

    • Prevents re-rendering of Component if props and state is the same
    • Takes care of “shouldComponentUpdate” implicitly
    • – It is the same as Component except that Pure Components take care of shouldComponentUpdate by itself, it does the shallow comparison on the state and props data. If the previous state and props data is the same as the next props or state, the component is not Re-rendered.

    • State and Props are Shallow Compared – standard arrays won’t work anymore as pushing a value results in the same array. What we need to do is use dot notation to create another array, then push the value
    • Pure Components are more performant in certain cases

    Similar to Pure Functions in JavaScript, a React component is considered a Pure Component if it renders the same output for the same state and props value. React provides the PureComponent base class for these class components. Class components that extend the React.PureComponent class are treated as pure components.

    Pure Components Restricts Re-rendering

    Let’s look at this Component where we extend from standard Component. It is a impure component because we did not extend from PureComponent.
    What makes it Impure?

    Notice we do setInterval where we do a setState every 1 second.

    When the state gets set, it changes, so our virtual DOM gets diff-ed. Once it diffs, it writes the changes to the real DOM. We do this every second. However, the UI never changes because nothing changes. We’re drawing the same thing over and over again. There is effectively no difference in the UI — the new values are unchanged. Re-rendering, in this case, is an overhead.

    In order to solve this problem, we introduce…

    Pure Components

    They compare the initial and final values for the state and props variables. If there is no difference between the two, they won’t trigger the re-rendering logic for the component.

    Each time the state or props are updated it compares the previous and next value — if the values are the same the Render function is not triggered. Performance is then improved by not calling “render” repeatedly.

    Props and State Changes are Shallow Compared

    In Pure Component, the changes in react “props” and “state” is Shallow Compared to know whether they have changed. If they have changed, it renders. If it doesn’t, it does not render.

    Before we proceed further, we need to understand the concept of Shallow Comparison in JavaScript. The values saved in the variable can be either a primitive or reference type.

    Example “var a = 10”, the value saved in the variable “a” is of primitive type.

    The data stored in Objects and Array can be referred to as Reference type data. Comparing Primitive Values is not a concern.

    Problems arise when we have reference values during the comparison.

    Because the comparison used is shallow (only compare values), then the == will return true. However, these are actually two different objects. So even though their value may the same, their address reference is different. They are two different objects.

    In this example, we’re only dealing with one object. We have two references pointing to it. Thus, value comparison between the two references will be the same.

    We can also use spread operator to create a new object, and then copy the values over.

    IN this example, we create a whole new object with reference cloneData pointing to it. We then copy all properties/values from userInfo into it.
    Thus in the end, we have two objects, with same properties and values. One has reference userInfo pointing to it. The other has reference cloneData pointing to it. cloneData == userInfo will return false because the references are pointing to different memory locations.

    This issue with Shallow Comparison

    Say we have a reference pointing to an object with an array.
    We then push 6 onto it. When the PureComponent does a comparison, it will see that the before state’s userArray (and after state’s userArray) are ‘==’ (value equal) because they are pointing to the same object.

    But its clear that they are not the same. Before state’s userArray is 1,2,3,4,5. After state’s userArray is 1,2,3,4,5,6.

    In the case when the props and state changes are made to primitive type variable,
    state and props changes to reference variable may lead to incorrect results and inconsistent rendering.

    The simple answer to resolving the shallow issue is to work with immutable data using dot operator

    In the example above, we can see that comparing reference variables can lead to a problem — only the address of the object are compared before and after the update. Since we’re comparing references, the address still remains the same and the Pure Component does not detect any changes and does not re-render. We can resolve this by creating a new instance of the state variable if it represents the reference type.

    we’re creating a new reference array using the spread operator. When the setState is called, the component looks for the reference of a previous array object and a new array object is created. The reference is different since this is a new instance of the array, so the object has a new address. Now the pure component will get to know that the data has been updated, the “render” life cycle will be invoked and the component will be re-rendered with correct values.

    Example

    React.memo() does the same as PureComponent but it does not check for changes in state, only in props. Hence, I will use React.memo in functional component here as an example.

    npx create-react-app my-app
    cd my-app
    npm start

    Then in /src, create MyPureComponent.js:

    Basically, we initialize an array with a, b, c. Every time we click, we add the next few letters d, e, f, g….etc. We do this by using number 100, incrementing it, and getting the ascii code for it. Then we append that letter onto the array.

    In index.js, we use our component.

    Run the app and start clicking the button.

    As you can see, after doing shallow comparison of the previous prop to current prop, it gets the same reference value of the object. Thus, it never renders.

    The fix here is to re-create the array and then do the push. That way, whenever you manipulate the array, you put the results onto a new array. Thus when the objects are compared, they are not the same, and thus triggers the render.

    The other thing you can do is to use React.memo’s second parameter with a areEqual function

    Let’s look at another example where we start off with NOT using a comparator. We are erroneously using two references to point to an array object. When we update this array, our sub component does not update because it does a shallow compare on the same array object.

    When we run it, we see that in the subcomponent, nothing gets re-render because its shallow comparison of prevProp and nextProp comes out to be true.

    We fix it by using […arr] so that it creates a whole new objectArray, then copying all the values over. Memo will shallowly compare against two different objects, which then will actually correctly re-render because it will return false due to the two array objects being different.

    Now as you add in additional letters, you’ll see that the sub component re-renders.

    Using Comparator Func

    The code stays the same. All we need to do is to make sure we stick a comparator function in our memo’s 2nd parameter.

    The handling of adding a letter stays the same.

    then create an areEqual comparator:

    We default it to return true because we don’t want it to re-render for now. We want to see the previous prop and next prop.

    Great. We see that there is a difference. This is because the previous array is [‘a’, ‘b’, ‘c’]. We setArr to a new Arr that is a totally different array object by using […arr].
    That way, we can see that both props are different.

    Now let’s implement areEqual. Because our previous and next array data are different, we can actually make some decisions. If they are of different length, we re-render. If they are of different characters, we re-render. Otherwise, we don’t render.

    Full Code

    Copy and paste into App.js

Store it into React state or Redux state?

ref – https://spin.atomicobject.com/2017/06/07/react-state-vs-redux-state/
https://stackoverflow.com/questions/39977540/can-redux-be-seen-as-a-pub-sub-or-observer-pattern

React state

React state is stored locally within a component. When it needs to be shared with other components, it is passed down through props.

In practice, this means that the top-most component in your app needing access to a mutable value will hold that value in its state.

If your top most component needs access to a value that is changeable, you need to hope it in React state. If this state is shared with other components, it needs to be passed through props.

If it can be mutated by subcomponents, you must pass a callback (into the subcomponent) to handle the update in the top-most component.

If your children components need to change this value, your parent component should pass a callback into your child component. That way when the child component changes the value, it can pass it back up to the parent component so it can update it. That way, the parent component is not holding the old value, while the child component has the newest updated value.

Redux state

The need of something like Redux when you have a more complicated application, with top-level components that need to talk to each other (actions) and somehow share some state.

Any component that needs access to a value may subscribe to the store and gain access to that value. Typically, this is done using container components. This centralizes all data but makes it very easy for a component to get the state it needs, without surrounding components knowing of its needs.

Redux is a library to manage shared state between components and to coordinate state mutations. Redux uses a pub/sub pattern.

It has a subscribe method that is used by components to subscribe to changes in the state tree. Normally you don’t use store.subscribe directly, as the Redux-React bindings (Redux’s connect basically) do that for you. The store then emits the changes.

Consider also that it’s perfectly fine to keep using the components internal state together with Redux. You can use the internal state to store state you don’t need/want to share with other components.

Some Guidelines on where to store state

Duration

Different pieces of state are persisted for different amounts of time. I loosely categorize time into these groups:

Short term: data that will change rapidly in your app

Short term data will likely change rapidly. At the simplest level, this includes the characters present in a text field as the user types. I would also extend this to include data in an entire form; this data can be considered a work in progress until the form is submitted. This type of data can be stored in local React state. I would even include things like how to filter a list of items, and whether or not to show or hide completed items in a to-do app (assuming it is not tied to user preferences).

Medium term: data that will likely persist for a while in your app

By medium-term state, I mean state that will stick around while the user navigates your app. This could be data loaded from an API, or any changes that you want to be persisted up until a page refresh.

However, after submitting a form, I would store the state in the Redux store. As an example, if a user submits a form updating their profile information, it would make sense to store this in Redux.

Long term: data that should last between multiple visits to your site

This is state that should be persisted between refreshes of the page or between separate visits to the page. Since the Redux store will be created anew when refreshing, this type of data should be stored somewhere else (likely to a database on a server or into local storage in the browser).

Breadth of Use

Another consideration is how many components in your React app need to access the state. The more state needs to be shared between different components in your app, the more benefit there is to using the Redux store. If the state feels isolated to a specific component or a small part of your app, React state may be a better option.

Depth of passing down props

React state should be stored in the most top-level component for which a subset of its subcomponents will need access to the state. Sometimes, this can mean many layers between the component storing the state and the subcomponents that render the data, and each layer means another prop must be propagated down the virtual DOM.

Parent-to-Child prop passing and callback propagating works great if you only need to pass the state down one or two levels into the subcomponents. But beyond that, it can feel tedious and require edits to many components every time you find a subcomponent needs access to new state. In cases like this, I find it much easier to store the state in Redux and use a container component to pluck the desired data from the store.

Unrelated components needing the same state

It could also be that multiple, relatively unrelated components need access to the same state. Take the above example of a form to update a user’s profile. The component here should receive the initial user profile in its props. You might also have a header component with a subcomponent that displays the user’s username or related data.

You could, of course, take passing down props to the extreme, in which your top-level component knows about the user’s profile and passes it to the header, which passes it to a subcomponent (and maybe further), and also passes it down to the profile-editing component. However, this requires a lot of management between the whole virtual DOM.

This type of solution is solved much more cleanly by storing the profile in the Redux store, and allowing container components around the header and profile-editing component to grab data from Redux’s store.

Ability to Track Changes to the State

Another time to choose Redux is when you need to track changes to state:

  • Maybe you want to replay events.
  • Maybe you want to implement undo/redo in your app.
  • Maybe you just want to log how state is changing.

In cases like these, Redux is a great solution; each action that is created is an artifact of how the state changes. Redux makes all these tasks simpler by centralizing them in a single store.

Refs (accessing the DOM)

ref – https://reactjs.org/docs/refs-and-the-dom.html

There are a few good use cases for refs:

  • Managing focus, text selection, or media playback.
  • Triggering imperative animations.
  • Integrating with third-party DOM libraries.

Refs are created using React.createRef() and attached to React JSX elements via the ref attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component.

When a ref is passed to an element in render, a reference to the node becomes accessible at the current attribute of the ref.

When the ref attribute is used on an HTML element, the ref created in the constructor with React.createRef() receives the underlying DOM element as its current property.

When the ref attribute is used on a custom class component, the ref object receives the mounted instance of the component as its current.
You may not use the ref attribute on function components because they don’t have instances.

In our CustomTextInput:

1) create a ref using React.createRef() and assign it to this.textInput

2) in render, assign an input control’s ref attribute to our this.textInput.
This creates a current property in our this.textInput and assigns the node of that input control to it.

3) For demo purposes, we want to show we can now control the input by programmatically putting a focus on it.
We do this by writing a method called focusTextInput. We create a button. Whenever that button is clicked, it sends
an event to our focusTextInput. In turn our focusTextInput uses our ref to programmatically put focus onto our input control.

You can also use it on component lifecycle functions like so:

However, you can only use it on class component because there is an instance.

In functional components, you cannot use it because there is no instance:

If you want to allow people to take a ref to your function component, you can use forwardRef.

You can, however, use the ref attribute inside a function component as long as you refer to a DOM element or a class component:

Forward Refs

Ref forwarding is a feature that lets some components take a ref they receive, and pass it further down to a child.

Callback Refs

React also supports another way to set refs called “callback refs”, which gives more fine-grain control over when refs are set and unset.

Instead of passing a ref attribute created by createRef(), you pass a function. The function receives the React component instance or HTML DOM element as its argument, which can be stored and accessed elsewhere.