ReactJS useState Hook

Rahul Bansal
Updated on 19-Mar-2021 11:02:51

437 Views

In this article, we are going to see how to use the useState hook in a functional component.State is the place where the data comes from. We should always try to make our state as simple as possible. With React 16.8, hooks are released which allow us to handle the state with less coding.Syntaxconst [state, setState] = useState(initialstate)It accepts two parameters – state and setState. State is the current state, while setState is used to change the state of the functional component.On every state change, the component re-renders with the updated state.ExampleIn this example, we will build a React application ... Read More

ReactJS useRef Hook

Rahul Bansal
Updated on 19-Mar-2021 11:00:49

636 Views

In this article, we are going to see how to create a reference to any DOM element in a functional component.This hook is used to access any DOM element in a component and it returns a mutable ref object which will be persisted as long as the component is placed in the DOM.If we pass a ref object to any DOM element, then the .current property to the corresponding DOM node elements will be added whenever the node changes.Syntaxconst refContainer = useRef(initialValue);ExampleIn this example, we will build a React application that passes the ref object to two input fields.When clicked ... Read More

React useReducer Hook

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

764 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

917 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

397 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

975 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

Array Within a Structure in C Programming

Bhanu Priya
Updated on 19-Mar-2021 10:47:59

5K+ Views

An array of structure in C programming is a collection of different datatype variables, grouped together under a single name.General form of structure declarationThe structural declaration is as follows −struct tagname{    datatype member1;    datatype member2;    datatype member n; };Here, struct is the keyword.tagname specifies the name of structure.member1, member2 specifies the data items that make up structure.ExampleThe following example shows the usage of array within a structure in C programming −struct book{    int pages;    char author [30];    float price; };ExampleFollowing is the C program to demonstrate the use of an array within a structure ... Read More

ReactJS useEffect Hook

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

900 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

299 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

872 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

Advertisements