Category Archives: React

react js

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.

Mosh Redux part 6 (reduxjs toolkit’s slice)

redux toolkit also has slices, which combines createAction and createReducer.

So it automatically creates actions and reducers for us.

questions.js
let’s import createSlice

createSlice takes an object with properties name, initialState, and reducers.
name represent the feature of this reducer.
intialState is the starting point of the state.
Finally, we have a reducers property, which references an object. Inside this object,

In the previous approach, questionAdded was an object, so we can use [questionAdded] to check for the type.

In this approach, questionAdded is just a property. Slices automatically creates actionTypes and actions for us.

let’s look at this slice object.

We see the properties. We need to default export the reducer and export the actions.

So with this implementation we can remove the previous actions and reducers.

also need to export actions. We do so by extracting actions from slice.actions

Mosh Redux part 5 (cleaner code with reduxjs toolkit)

source code

Here we talk about a powerful library called redux toolkit

Open up a terminal and type:
npm i @reduxjs/toolkit

Redux toolkit provides a bunch of tools to simplify redux code.

One of them is for creating the store. Typically, we extract createStore from redux. Then put your reducers into createStore as a higher order function. We export this store to be used in our index.

If we use createStore way, we have do a lot of stuff manually.

But with redux tookit, we have a new way:

configureStore

We import our reducer(s) from our feature and insert via an object with property reducer:

A pro on using configureStore is that allows us to dispatch asynchronous actions.

Actions with less boilerplate

Typically we create actions by defining constants, then define objects with properties type and payload. The type property uses the defined constants.

However, there is a problem. Every action returns an action like so:

We manually create these by hand.
Redux toolkit has a function called createAction to do this for us.

Hence, in your action creator, we re-factor like so:

Now, in your reducer you can replace the type with our createAction.type:

Notice this:

When we use createAction to create our action object, the parameter we give to createAction is the payload object. Make sure we have properties ‘description’.

make changes in your index.js:

Improving the Reducers

A lot developers don’t like switch statements. Nor do they like returning immutable object patterns.
This is where we can use toolkit. We can create reducer without switch statements. Toolkit uses immer. Automatically translated to immutable update pattern.

Similar to createAction, we use library function createReducer.

When we call this parameter, the first parameter is the initial state

Under the hood, redux toolkit uses immer, so the manipulation of your state gets translated to immutable updated pattern.

Reminder: immer uses function product with initial state. It takes an argument draftState

draftState is actually a proxy. So it will record all changes applied to a copy of the initial state. Thus, preventing the manipulation of the original object.

Thus, our function:

is actually the draftState anonymous function that does the mutating.

By using the functionality from redux toolkit, you can streamline your code nicely:

question.js

Mosh Redux Part 3 (Your own Custom Store)

Before moving, we should familiarize ourselves with how Redux is written from scratch.

Previously, when we log the store, we see the public interface with three main functionality:

– getState
– dispatch
– subscribe

Let’s try to implement them.

My Store

We hide our store variable in myStore. It is private.

Dispatch

Our reducer function gets passed into our constructor.
So we add a reducer parameter.

We create a dispatch function. So by scope, we access the passed in reducer function, and pass in action, and state. Remember in our reducer, we always return the new state. So we reference our state to the returned state.

Notify Subscribers (UI components)

When the state of the store gets changed, we want to notify the UI components. Hence we need these UI components be able to subscribe to our store.