getDerivedStateFromError Method in ReactJS

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

506 Views

In this article, we are going to see how to execute a function if some error occurs in the component.This method is called when a component encounters some error during the React Component Lifecycle. This method allows us to handle the error boundaries of the application. To avoid performance issues, don’t set up any side-effects in this method.Syntaxstatic getDerivedStateFromError(error)It accepts the error as a parameter that was thrown as a component.ExampleIn this example, we will build a React application that displays the contained Comp1 component if no error occurs; otherwise it displays some text. But here, in Comp1 component, error ... Read More

Delete Ith Index Node at Mid Level in Linked List using Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:53:28

149 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − When index == 0, then return head.nextStep 4 − Else, iterate the given linked list from head.Step 5 − If index i matches with the given index (to be deleted), then delete that node.next, break the loop.Step 6 − Return at the end of the loop.Example Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node ... Read More

ReactJS forceUpdate Method

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

764 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

234 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

972 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

Delete Node at 0th Position in a Linked List Using Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:50:13

118 Views

ExamplesApproach to solve this problemStep 1 − Define a method that accepts head of the linked list.Step 2 − If head == nil, return the head.Step 3 − When index == 0, then return head.nextStep 4 − Else, Iterate given linked list from head.Step 5 − If index i matches with given index(to be delete) then delete that node.next, break the loop.Step 6 − Return, at the end of loop.Example Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value   ... 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

Postorder Traversal of a Tree in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:46:29

316 Views

ExampleSuppose we have the following binary tree.Postorder Tree Traversal Output − 2 4 5 3 6 7 1.Approach to solve this problemStep 1 − If the root node of the given tree is nil, then return; else, follow the steps given below.Step 2 − Traverse the Left sub-tree.Step 3 − Traverse the Right sub-tree.Step 4 − Print the root node data.Example Live Demopackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PostOrderTraversal(){    if root !=nil{       root.left.PostOrderTraversal()       root.right.PostOrderTraversal()       fmt.Printf("%d ", root.data) ... 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

Update Node Value at Nth Position in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:44:32

100 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − Initialize the index as i := 0.Step 4 − Iterate the given linked list from its head.Step 5 − If index i matches with the given index (to be updated), then update that node.Step 6 − Else, return head.Example Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = ... Read More

Advertisements