Sunday 16 May 2021

useReducer in React

const [state, dispatch] = useReducer(reducer, initialArg, init);

An alternative to useState. Accepts a reducer of type (state, action) => newState, and returns the current state paired with a dispatch method. (If you’re familiar with Redux, you already know how this works.)

useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. useReducer also lets you optimize performance for components that trigger deep updates because you can pass dispatch down instead of callbacks.

Here’s the counter example from the useState section, rewritten to use a reducer: 



const initialState = { count: 0 };

function reducer(state, action) {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
case "decrement":
return { count: state.count - 1 };
default:
throw new Error();
}
}

function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "decrement" })}>-</button>
<button onClick={() => dispatch({ type: "increment" })}>+</button>
</>
);
}



Specifying the initial state

There are two different ways to initialize useReducer state. You may choose either one depending on the use case. The simplest way is to pass the initial state as a second argument:


  const [state, dispatch] = useReducer(
    reducer,
    {count: initialCount}  );

Lazy initialization

You can also create the initial state lazily. To do this, you can pass an init function as the third argument. The initial state will be set to init(initialArg).

It lets you extract the logic for calculating the initial state outside the reducer. This is also handy for resetting the state later in response to an action:

function init(initialCount) {  return {count: initialCount};}
function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    case 'reset':      return init(action.payload);    default:
      throw new Error();
  }
}

function Counter({initialCount}) {
  const [state, dispatch] = useReducer(reducer, initialCount, init);  return (
    <>
      Count: {state.count}
      <button
        onClick={() => dispatch({type: 'reset', payload: initialCount})}>        Reset
      </button>
      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
      <button onClick={() => dispatch({type: 'increment'})}>+</button>
    </>
  );
}

  • Using useMemo( ) -
    It is a React hook that is used for caching CPU-Expensive functions.
    Sometimes in a React app, a CPU-Expensive function gets called repeatedly due to re-renders of a component, which can lead to slow rendering.
    useMemo( ) hook can be used to cache such functions. By using useMemo( ), the CPU-Expensive function gets called only when it is needed.

  • Using React.PureComponent -
    It is a base component class that checks state and props of a component to know whether the component should be updated.
    Instead of using the simple React.Component, we can use React.PureComponent to reduce the re-renders of a component unnecessarily.

  • Maintaining State Colocation -
    This is a process of moving the state as close to where you need it as possible.
    Sometimes in React app, we have a lot of unnecessary states inside the parent component which makes the code less readable and harder to maintain. Not to forget, having many states inside a single component leads to unnecessary re-renders for the component.
    It is better to shift states which are less valuable to the parent component, to a separate component.

  • Lazy Loading -
    It is a technique used to reduce the load time of a React app. Lazy loading helps reduce the risk of web app performances to minimal.

Sunday 9 May 2021

Multi-level groupBy Using underscorejs or lodash

                # nest.js



                A multi-level [groupBy](http://underscorejs.org/#groupBy) 
                for arrays inspired by D3's [nest](https://github.com/mbostock/d3/wiki/Arrays#-nest) operator.



                Nesting allows elements in an array to be grouped into a hierarchical tree

                structurethink of it like the `GROUP BY` operator in SQLexcept you can have

                multiple levels of groupingand the resulting output is a tree rather than a

                flat tableThe levels in the tree are specified by key functions.



                See [this fiddle](http://jsfiddle.net/V7an5/3/) for live demo.





                ## Implementation



                Depends on lodash's [groupBy](http://lodash.com/docs#groupBy) and
                 [mapValues](http://lodash.com/docs#mapValues):



                ```js

                _ = require('lodash');



                var nest = function (seq, keys) {

                    if (!keys.length)

                        return seq;

                    var first = keys[0];

                    var rest = keys.slice(1);

                    return _.mapValues(_.groupBy(seq, first), function (value) { 

                        return nest(value, rest)

                    });

                };



                module.exports = nest;

                ```





                ## Usage



                Input data to be nested:



                ```js

                var data = [

                { type: "apple", color: "green", quantity: 1000 }, 

                { type: "apple", color: "red", quantity: 2000 }, 

                { type: "grape", color: "green", quantity: 1000 }, 

                { type: "grape", color: "red", quantity: 4000 }

                ];

                ```



                Key functions used for grouping criteria:



                ```js

                var byType = function(d) {

                return d.type;

                };



                var byColor = function(d) {

                return d.color;

                };



                var byQuantity = function(d) {

                return d.quantity;

                };

                ```





                ## First Example



                Expected output when grouping by `color` and `quantity`:



                ```js

                var expected = {

                green: {

                    "1000": [

                    { type: 'apple', color: 'green', quantity: 1000 }, 

                    { type: 'grape', color: 'green', quantity: 1000 }

                    ]

                },

                red: {

                    "2000": [

                    { type: 'apple', color: 'red', quantity: 2000 }

                    ],

                    "4000": [

                    { type: 'grape', color: 'red', quantity: 4000 }

                    ]

                }

                };

                ```



                Nest by key name:



                ```js

                deepEqual(nest(data, ['color', 'quantity']), expected);

                ```



                Nest by key functions:



                ```js

                deepEqual(nest(data, [byColor, byQuantity]), expected);

                ```





                ## Second Example



                Expected output when grouping by `type` and `color`:



                ```js

                expected = {

                apple: {

                    green: [ { "type": "apple", "color": "green", "quantity": 1000 } ],

                    red: [ { "type": "apple", "color": "red", "quantity": 2000 } ]

                },

                grape: {

                    green: [ { "type": "grape", "color": "green", "quantity": 1000 } ],

                    red: [ { "type": "grape", "color": "red", "quantity": 4000 } ]

                }

                };

                ```



                Nest by key names:



                ```js

                deepEqual(nest(data, ['type', 'color']), expected);

                ```



                Nest by key functions:



                ```js

                deepEqual(nest(data, [byType, byColor]), expected);

                ```