Found 90 Articles for ReactJS

ReactJS – forceUpdate() Method

Rahul Bansal
Updated on 18-Mar-2021 11:52:30

637 Views

In this article, we are going to see how to execute a function by forcibly re-rendering a component.The component in the React lifecycle only re-renders if the props passed to it or its state changes but to forcibly render the component, use the build it forceUpdate method. This method overrides the shouldComponentUpdate method defined in the component but will consider the shouldComponentUpdate method defined in the children component.Syntaxcomponent.forceUpdate(callback)ExampleIn this example, we will build a React application that gets forcibly re-rendered on a button click.App.jsximport React from 'react'; class App extends React.Component {    update = () => {   ... Read More

ReactJS Developer Tools

Rahul Bansal
Updated on 18-Mar-2021 11:51:51

163 Views

While building a React application, the most-used Chrome extension for debugging the React application or to solve the error is React Developer Tools which is a free-to-use and opensource chrome extension.This extension is used to navigate through the nested component tree of the React Components. It takes a lookup into the stored state and the props values and also records the performance information.Note: This extension also tells whether a page is using the technology stack of ReactJS or not.How to installGo to Chrome web store and install React Developer Tools.When you tap on this extension, it will show if the ... Read More

ReactJS – createRef() method

Rahul Bansal
Updated on 18-Mar-2021 11:50:26

844 Views

In this article, we are going to see how to create a reference to any DOM element in the functional component.This method 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.SyntaxReact.createRef()ExampleIn this example, we will build a React application that will pass the ref object to two input fields and when ... Read More

ReactJS – componentWillUpdate() Method

Rahul Bansal
Updated on 18-Mar-2021 11:47:13

4K+ Views

In this article, we are going to see how to execute a function before the component is updated in the DOM tree.This method is used during the updating phase of the React lifecycle. This function is generally called before the component is updated or when the state or props passed to the component changes. Don’t call setState() method in this function. This method will not be invoked if shouldComponentUpdate() methods return false.Note: This method is now deprecated.SyntaxUNSAFE_componentWillUpdate(nextProps, nextState)ExampleIn this example, we will build a color changing React application which calls the componentWillUpdate method when the component is updated in the DOM ... Read More

ReactJS – componentWillUnmount() Method

Rahul Bansal
Updated on 18-Mar-2021 11:45:58

5K+ Views

In this article, we are going to see how to execute a function when the component is deleted from the DOM tree.This method is called during the unmounting phase of the React Lifecycle, i.e., before the component is destroyed or unmounted from the DOM tree. This method is majorly used to cancel all the subscriptions that were previously created in the componentWillMount method.Never call this.setState() inside the componentWillUnmount method as this component is never going to be re-rendered again.SyntaxcomponentWillUnmount()ExampleIn this example, we will build a React application which displays the list of all users. On clicking the 'Delete User' button, ... Read More

ReactJS – componentWillReceiveProps() Method

Rahul Bansal
Updated on 18-Mar-2021 11:44:09

6K+ Views

In this article, we are going to see how to execute a function if the props passed to the component is updated in the DOM tree.This method is used during the updating phase of the React lifecycle. This function is generally called if the props passed to the component change. It is used to update the state in response with the new received props. setState() method doesn’t generally call this method again.Note: This method is now deprecated.SyntaxUNSAFE_componentWillReceiveProps(nextProps)ExampleIn this example, we will build a color-changing React application which will call the componentWillReceiveProps method when the props of the component are updated.Our first ... Read More

ReactJS – componentWillMount() Method

Rahul Bansal
Updated on 18-Mar-2021 11:42:07

2K+ Views

In this article, we are going to see how to execute a function before the component is loaded in the DOM tree.This method is used during the mounting phase of the React lifecycle. This function is generally called before the component gets loaded in the DOM tree. This method is called before the render() method is called, so it can be used to initialize the state but the constructor is preferred.This method is generally used in server-side rendering. Don’t call subscriptions or side-effects in this method; use componentDidMount instead.Note: This method is now deprecated.SyntaxUNSAFE_componentWillMount()ExampleIn this example, we will build a ... Read More

ReactJS – componentDidUpdate() Method

Rahul Bansal
Updated on 18-Mar-2021 11:40:38

1K+ Views

In this article, we are going to see how to execute a function when the component is updated in the DOM tree.This method is called only when the component gets updated or when the props passed to it change. It isn’t called for the initial render of the component. This method is majorly used to perform some operations which are needed to be executed only if the DOM is updated.To avoid any performance issues, it is advised to use this method with conditional loop like −componentDidUpdate(prevProps, prevState) {    if (prevState.text !== this.state.text) {       // Write logic ... Read More

ReactJS – componentDidMount Method

Rahul Bansal
Updated on 18-Mar-2021 11:38:36

817 Views

In this article, we are going to see how to execute a function when the component is loaded in the DOM tree.This method is majorly used during the mounting phase of the React lifecycle to handle all the network requests or to set up all the major subscriptions of the application.You can always set up network requests or subscriptions in the componentDidMount method but to avoid any performance issues, these requests are needed to be unsubscribed in the componentWillUnmount method which is called during the unmounting phase of the React lifecycle.SyntaxcomponentDidMount()ExampleIn this example, we will build a color-changing React application ... Read More

ReactJS componentDidCatch method

Rahul Bansal
Updated on 18-Mar-2021 11:34:31

988 Views

In this article, we are going to see how to execute a function in the commit phase if some error occurs in the Component.This method is called when a component or any children component encounters some error during the React Component Lifecycle. This method allows us to handle the error boundaries of the application.Unlike the getDerivedStateFromError method, this method is called during the commit phase, so side-effects are also allowed in this method.SyntaxcomponentDidCatch(err, data)Parameterserr - Error thrown by the component.data - Stores information about the component which has thrown the error.ExampleIn this example, we will build a React application that displays the ... Read More

Advertisements