Found 217 Articles for Javascript Library

JSX in depth in React.js

Shyam Hande
Updated on 05-Sep-2019 08:00:25

216 Views

JSX simply creates a React element using createElement method in the end.Example Submit Will be converted to −React.createElement(    FormButton,    {color: 'green', buttonSize: 10},    'Submit’ )Self-closing tags are also possible to add.Capitalizing the custom React ElementThe custom react element must be named with first character in capital so that React will be able distinguish between the html element and the react element.As Jsx gets converted to React.createElement, the React library must be in scope. For that we have to import the React if jsx needs to use.Accessing the inner properties using dot operatorThe inner properties of an element ... Read More

Higher order components in React.js

Shyam Hande
Updated on 05-Sep-2019 07:53:53

147 Views

Higher order component in short called as hoc. It’s a pattern which receives a component and returns a new component with add-on features to it.//hoc is the name of a custom JavaScript functionconst AddOnComponent= hoc(SimpleComponent);We use component with state and/or props to build an UI. Similar way a hoc builds a new component from the provided component.Use of hoc is making a cross cutting concerns in React. The components will take care of individual responsibility of single tasks while hoc functions will take care of cross cutting concerns.Connect function from redux is an example of hoc.A practical example of hocDisplay ... Read More

Fragment in React.js

Shyam Hande
Updated on 05-Sep-2019 07:48:28

367 Views

Most of the times we need to return multiple elements from a component. React Fragment helps in returning multiple elements. The other alternative is to use a html element like div to wrap them. But using extra html node can cause some semantic issues.Example of React.Fragmentrender() {    return (                                  ); } The short syntax is : render() {    return (                                 ... Read More

Forwarding ref in React.js

Shyam Hande
Updated on 05-Sep-2019 07:45:54

299 Views

Passing a ref to the child component is called as forwarding ref. React provides a createRef function to create a ref for element.forwardRef is a function to pass the ref to child component.Example// ExampleRef.js const ExampleTextInput = React.forwardRef((props, ref) => (     )); const exampleInputRef = React.createRef(); class NewTextInput extends React.Component {    handleFormSubmit = e => {       e.preventDefault();       console.log(“Entered value: ”+exampleInputRef.current.value);    };    render() {       return (                       this.handleFormSubmit(e)}>                 ... Read More

Context api in React.js

Shyam Hande
Updated on 04-Sep-2019 14:41:40

743 Views

The React context api is safe to use in production with the version 16.3 or latest. The reason for adding context api is to avoid the passing of props if there is a chain of children components.Without the use of context api, we have to pass the props through all the intermediate components. The other alternative solution is to use third party library such as Redux for maintaining a central store.Understanding the passing of props problemApp.js → props for books → BookList.js → passing the books as props again → Book.jsWith the increase in number of children components, the chain ... Read More

Why need build workflow in React.js

Shyam Hande
Updated on 04-Sep-2019 14:34:28

611 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
Updated on 04-Sep-2019 14:24:52

469 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

Using useEffect() in React.js functional component

Shyam Hande
Updated on 04-Sep-2019 14:22:45

3K+ Views

The React hook useEffect helps in adding componentDidUpdate and componentDidMount combined lifecycle in React’s functional component.So far we know we can add lifecycle methods in stateful component only.To use it, we will need to import it from react −import React, { useEffect } from ‘react’; const tutorials=(props)=>{    useEffect( ()=>{       console.log(‘hello’);       setTimeout( ()=>{ alert(‘hello’); }, 2000);    }); }If we run it, we will see the console log and alert on every render cycle. Here we can call http requests also inside useEffect. Now this is similar to componentDidUpdate lifecycle of stateful component.We can ... Read More

Using useContext in React.js

Shyam Hande
Updated on 04-Sep-2019 14:20:48

824 Views

useContext hook allows passing data to children elements without using redux.useContext is a named export in react so we can importin functional components like below −import {useContext} from ‘react’;It’s an easy alternative to Redux if we just need to pass the data to the children elements.Simple example creating a contextimport React, { createContext } from ‘react’; import ReactDOM from ‘react-dom’; const MessageContext = createContext(); myApp=()=>{    return (                                                ); }In child component Test we can ... Read More

Using proptypes in React.js

Shyam Hande
Updated on 04-Sep-2019 14:44:55

150 Views

Use of proptypes ensures the type safety of receiving props on the components and also helps in making correct calculation.Example − If we are receiving name as string and age as number then it should be received with the same type. If we receive age in string then it can result into incorrect calculation.To use proptypes we have to install below package.npm install –save prop-typesThis package is provided by React Team. To use it on component, we will import it firstimport PropType from ‘prop-types’;we can use it on any type of component (Stateful or stateless).At the end of component before exporting ... Read More

Advertisements