Found 90 Articles for ReactJS

Validate URL in ReactJS

Rahul Bansal
Updated on 19-Mar-2021 11:15:50

2K+ Views

In this article, we are going to see how to validate a URL (Uniform Resource Locator) in a React application.To validate a URL, we are going to install a third-party package of ‘validator’ which is used to validate the URL. Example of a valid and an invalid URL are as follows −Valid URL − https://www.tutorialspoint.com/Invalid URL − https://www.tutorialspointInstalling the modulenpm install validatorORyarn add validatorNpm is the node package manager which manages our React package but yarn is the more secure, faster and lightweight package manager.ExampleIn this example, we will build a React application which takes a URL input from the ... Read More

Using onKeyPress event in ReactJS

Rahul Bansal
Updated on 19-Mar-2021 11:15:29

3K+ Views

In this article, we are going to see how to access the keycode which the user has pressed in a React applicationTo detect the keycode or the key which the user has pressed from his keyboard then React has a predefined onKeyPress event for this. Although it is not fired for all keys like ALT, CTRL, SHIFT, ESC but it can detect other keys properly.The order of event firing is −onKeyDown − Fired when the user is pressing the keyonKeyPress − Fired when the user has pressed the keyonKeyUp − Fired when the user releases the keyExampleIn this example, we will ... Read More

Suspense in ReactJS

Rahul Bansal
Updated on 19-Mar-2021 11:15:05

890 Views

In this article, we will learn how to show a loader while the component is being lazily loaded.When the components are lazily loaded, it requires a fallback to be shown to indicate that the component is being loaded in the DOM.SyntaxExampleIn this example, we will build a Routing application that lazily loads the component and displays a loader while the component is being loaded lazily.App.jsximport React, { Suspense, lazy } from 'react'; import { BrowserRouter as Router, Route } from 'react-router-dom'; import Loader from './Loader.js'; const Contact = lazy(() => import('./Contact')); const App = () => (   ... Read More

Strict Mode in ReactJS

Rahul Bansal
Updated on 19-Mar-2021 11:12:27

606 Views

In this article, we are going to see how to highlight potential problems that we might have in a React Application.React.StrictMode is a helper functionality provided by React which allows us to write better React codes. It provides visual feedback in the form of warnings if we don’t follow the React guidelines, but it works only in the development mode.Note: They are unsafe to use while using async await.Use-cases of Strict Mode −Identifying components with unsafe lifecyclesWarning about legacy string ref API usageWarning about deprecated findDOMNode usageDetecting unexpected side effectsDetecting legacy context APIWe can also bind React.StrictMode to work only ... Read More

Sending Http Requests in ReactJS

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

841 Views

In this article, we are going to learn how to send and receive Http Responses in a React application.To send or receive data, we don’t need to use third-party packages, rather we can use the fetch() method which is now supported by all the modern browsers.Sending GET requesthttps://jsonplaceholder.typicode.com/todos/1Jsonplaceholder is a fake API which is used to learn the process of sending requests.Exampleindex.jsximport React from "react"; import ReactDOM from "react-dom"; import { CookiesProvider } from "react-cookie"; import App from "./App"; ReactDOM.render(              ,    document.getElementById('root') );App.jsximport React, { useEffect, useState } from 'react'; const ... Read More

RegEx in ReactJS

Rahul Bansal
Updated on 14-Sep-2023 02:10:28

28K+ Views

In this article, we are going to see how to handle the strings with RegEx handling in a React application.A RegEx or Regular Expression is a sequence of characters that forms a search pattern and is used to check if a string contains a specified search pattern or not. It is also used to validate strings which consist of email, passwords etc.Syntaxnew RegExp(pattern[, flags])ExampleIn this example, we will build an authentication React application that takes an email and password from the user and checks if they are validated or not.We have Regex.js which contains all the regular expressions to validate ... Read More

ReactJS – useState hook

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

318 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

464 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

ReactJS – useReducer hook

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

574 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

612 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

Previous 1 ... 3 4 5 6 7 ... 9 Next
Advertisements