Front End Technology Articles

Page 619 of 652

Why need build workflow in React.js

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 790 Views

BUILDING A WORK-FLOW HELPS IN DOING BELOW THINGSIt optimizes codeUsing next-gen JavaScript (ES6)Its a standard way for single/multiple page appsProductive approachEasy integration of dependencies with NPM or YarnUse of bundler like web-pack for easier modular code and shipping codePre compiler like BabelWe can use a local development server to test appBuilding workflow looks complex but React community has made it simple for us with a single commandcreate-react-app.To use create-react-app, we will need to have node js install on our machine.You can check if node is installed using below command on terminal −node –versionIf not installed already, please install the latest ...

Read More

Using useState hook in React.js

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 653 Views

Hooks allows the functional component in react to get the features available in class based component in make them more powerful.useState we will import from react. Import {useState} from ‘react’; This helps us in creating local state variables for functional component and provides method to update that variable.State in class is an object, but with useState we can create simple primitive data types and object if we want.const test=()=>{    const [age, setAge] = useState(25);    return (                Age: {age}          setAge(age+1)}>Increase Age          ); ...

Read More

Promises Callbacks And Async/Await

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 1K+ Views

First we have to understand two main conceptsSynchronous programmingAsynchronous programmingSYNCHRONOUS PROGRAMMINGIt waits for each statement to complete its execution before going to next statement.This approach can slow down application process if statements are not dependent on each other but still they are waiting for execution as they are in queue.ASYNCHRONOUS PROGRAMMINGIt does not wait for current statement to complete its execution before moving to next statement. e.g. calling a web service and executing file copy in JavaScript.The call to web service can take some time to return a result meanwhile we can complete some other actions.Once server provides the result, ...

Read More

Returning adjacent element in React.js and HOC

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 336 Views

Generally the return statement in React’s render method returns a single div element enclosing all the child jsx like below −render(){    return (                Header          Test          ); }Here we cannot simply return multiple elements, we need to have a parent container similar to div as shown above.If no parent container element, then we can even return an array like below −While returning an array we will require an unique key to be given to each element of an arrayrender(){    return (   ...

Read More

React.js routing

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 2K+ Views

Before React Router v4, the routing in react was static but after v4 update its dynamic. In single page applications, we have only one page index.html and different components which gets displayed based on routes.In non spa web applications, server returns a page on request. To start with routing , first install the routing package −npm install –save react-router-domOnce we create the create the project with create-react-app, we will see there is only one html file and that is index.html and a single component named as AppNow, we will create two more components AboutUs.jsx and ContactUs.jsxAboutUs.jsximport React from 'react' class ...

Read More

React.js component lifecycle error handling phase

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 903 Views

There are two main methods in error handling. These method are used in the error boundary mechanism in React. We wrap the component ( from where there is a possibility of error occurrences ) in a class which handles the below methods.Static method getDerivedStateFromError(error)componentDidCatch(error, info)Static getDerivedStateFromError(error): As name of the method suggest, we can update state object here based on error received from descended component.componentDidCatch(error, info): we can log the errors using api call. This is helpful in displaying useful error message on screen instead of technical errors.A class can be termed as an error boundary if it implements at ...

Read More

React.js Component Lifecycle - Updating phase

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 435 Views

The update lifecycle can be triggered by two events −Update of props by parent componentChange in local stateUpdate of props by parent componentcomponentWillReceiveProps(nextProps) −This is the first method which gets called on prop change. It contains one argument nextProps means the newly changed props.Here, we can synchronize local state with props if requiredIf state is not needed to synchronize with props then probably no need to implement this method.Causing side effects should be avoided here. Because if side effects used here, it may results into re-render or performance issues.Now, componentWillReceiveProps is deprecated and replaced by static method getDerivedStateFromProps(props, state). As ...

Read More

React.js Component Lifecycle - Unmounting

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 426 Views

ComponentWillUnmount is the only method that executes in unmount phase.Component enters into this phase when there is no matching in element tree for this component.Just before the component gets removed from actual DOM, this method gets called.Along with removal of this component from DOM tree, all children of this component also gets removed automatically.Once component is removed from the DOM, it gets available for the garbage collection in React.Cleanup activities can be done in this method. E.g. clear localstorage variables used in app, clear session, clean up charts, cleanup timers, cancel pending api requests etc.componentWillUnmount(){    this.resetSession(); //example method to ...

Read More

Pure Component in React.js

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 2K+ Views

We have a lifecycle method called shouldComponentUpdate which by default returns true (Boolean) value.The purpose of the shouldComponentUpdate is we can custom implement the default behavior and decide when react should update or re-render the component.Generally we use state or props value to decide the update cycle. React has now provided us a PureComponent which does the comparison of state and props to decide the update cycle. We don’t need to override shouldComponentUpdate if we extend class with PureComponent.React does the shallow comparisons of current state and props with new props and state to decide whether to continue with next ...

Read More

Nested Routing in React.js

Shyam Hande
Shyam Hande
Updated on 04-Sep-2019 504 Views

We have an app.jsx componentimport React, { Component } from 'react'; import { Link, Route, Switch } from 'react-router-dom'; import Category from './Category'; class App extends Component {    render() {       return (                                                         Main Page                   Users                                           ...

Read More
Showing 6181–6190 of 6,517 articles
« Prev 1 617 618 619 620 621 652 Next »
Advertisements