Found 8894 Articles for Front End Technology

ReactJS – useReducer hook

Rahul Bansal
Updated on 19-Mar-2021 10:58:54

607 Views

This hook is a better alternative of the useState hook, as it is used when we want to attach a function along with handling the state or when we want to handle the state based on the previous values.Syntaxconst [state, dispatch] = useReducer(reducer, initialArgs, init);ParametersReducer − The function to handle or update the stateinitialArgs − Iitial stateInit − To load the state lazily or when it is requiredExampleIn this example, we will build a simple calculator using the useReducer hook which takes the input from the user and displays the result accordingly.App.jsximport React, { useReducer } from 'react'; const ... Read More

ReactJS – useMemo hook

Rahul Bansal
Updated on 19-Mar-2021 10:55:25

654 Views

In this article, we are going to see how to optimize a React application by passing a memoized value.This hook is used to optimize the React application by returning a memoized value which helps to prevent doing the complex calculations on every re-rendering. This hook stores the cached value and only updates the function on certain defined conditions.Note: Don’t call side-effects in useMemo hooks; use useEffect hook instead.Syntaxconst memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);Here, the returned value of computeExpensiveValue() function will only be changed on the next re-render if the values of a or b change.Without useMemo HookExampleIn ... Read More

ReactJS – useLayoutEffect hook

Rahul Bansal
Updated on 19-Mar-2021 10:52:31

298 Views

In this article, we are going to see how to set up side-effects or HTTP requests in a functional component.This hook has the similar functioning like that of useEffect hooks but rather than being called out asynchronously, it has a synchronous effect. This hook is used to load the data in the DOM synchronously and also works in the same phase like that of useEffect hook.Note: Use useLayoutEffect hook only if useEffect hooks don't give the expected output.SyntaxuseLayoutEffect()ExampleIn this example, we will build a React application that displays and updates the name synchronously.App.jsximport React, { useLayoutEffect, useState } from 'react'; ... Read More

ReactJS – useImperativeHandle hook

Rahul Bansal
Updated on 19-Mar-2021 10:50:36

795 Views

In this article, we are going to see how to customize the instance value of the ref object.Both useImperativeHandle and useRef hook allows to pass the ref object but the latter one doesn’t allow to customize the instances that are also passed with the ref object. useImperativeHandle hooks is different from the useRef hook in majorly two ways −It allows handling and customizing the returned value explicitly.It allows you to replace the native instances of the ref object with the user-defined ones.SyntaxuseImperativeHandle(ref, createHandle, [deps])ExampleIn this example, we are going to build a custom Button Component that has a user-defined instance attached ... Read More

ReactJS – useEffect hook

Rahul Bansal
Updated on 19-Mar-2021 10:47:57

651 Views

In this article, we are going to see how to set up side-effects or HTTP requests in a functional component.This hook is used to set up subscriptions, handle side-effects or to send analytics data to the server. It is the combination of componentDidMount, componentDidUpdate and componentWillUnmount methods of class-based components. The function passed to this hook will run only after the component is rendered.SyntaxuseEffect(()=>{}, []);()=>{} − Function passed to this hook[ ] − It tells the hook when to re-render the component. For example −[props] − If props values are changed then this hook is called again.[ ] − This ... Read More

ReactJS – useDebugValue hook

Rahul Bansal
Updated on 19-Mar-2021 10:47:33

215 Views

In this article, we are going to see how to debug the custom hooks with the useDebugValue hook in ReactJS.This hook provides custom labels to custom hooks so as to make the process of debugging easier and efficient. It is only called when the React Developer tools are toggled on.SyntaxuseDebugValue(value, ()=>{})ParametersValue − Label for the custom hook.()=>{} − Function to format the label.ExampleIn this example, we will build a React application that displays a custom label for the custom hook of our React application.App.jsxfunction useCustomHook(val) {    const [value, setValue] = useState(null);    useDebugValue(value ? Not Empty : Empty);   ... Read More

ReactJS – useContext hook

Rahul Bansal
Updated on 19-Mar-2021 10:46:59

607 Views

In this article, we are going to see how to access the data without passing it through every parent component in the React Lifecycle.This hook is the better replacement of the Context API of the Class-based component which is used to set the global data and this data can now be accessed in any of the children components without the need to pass it through every parent Component.Syntaxconst authContext = useContext(initialValue);The useContext accepts the value provided by React.createContext and then re-render the component whenever its value changes but you can still optimize its performance by using memoization.ExampleIn this example, we ... Read More

Counting pairs with range sum in a limit in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:17:22

200 Views

Range SumRange sum rangeSum(i, j) is defined as the sum of the elements in an array between indices i and j (i ≤ j), inclusive.ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and two numbers, upper and lower as the second and third element.Our function is supposed to return the number of range sums that lie between the range [upper, lower], (both inclusive).For example, if the input to the function is −const arr = [1, 4, 3]; const upper = 5; const lower = 2;Then the output should ... Read More

Longest path in 2-D that contains increasing sequence in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:15:12

249 Views

Increasing SequenceA sequence of numbers in which each succeeding element is either greater or equal to the preceding element is an increasing sequence.For instance, 4, 6, 8, 9, 11, 14 is increasing sequence 3, 3, 3, 3, 3, 3, 3 is also an increasing sequenceProblem:We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument. Our function should find and return the length of that longest path in the array that contains only increasing numbers.For example, if the input to the function is −const arr = [    [4, 5, ... Read More

Adding elements to array to make its sum diverse in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:10:49

56 Views

ProblemWe are required to write a JavaScript function that takes in an array of Numbers, arr, as the first argument and a single number, num, as the second argument.We should, by adding elements to it, make our array such that any sum can be obtained by adding specific numbers from it between [0, num] (including both). Our function should finally return the minimum number of numbers required to add to the array so that it can produce any sum between 0 and num.For example, if the input to the function is −const arr = [1, 5, 10]; const sum = ... Read More

Advertisements