Found 224 Articles for Javascript Library

Forwarding ref in React.js

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

411 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

908 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

724 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

603 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

1K+ 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

250 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

Promises Callbacks And Async/Await

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

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

Styling in React.js

Shyam Hande
Updated on 04-Sep-2019 13:50:26

617 Views

Styling in React.js can be done in below two wayscss style sheetsinline styleLet’s see CSS style sheets firstWe have App.js file as shown below −import React, {Component} from 'react'; import './App.css'; class App extends Component {    render(){       return (                       Styling React Components                   );    } } export default App;In App.js file we have imported an App.css file which contains css class myColoredTextPlease noteWe used name of css class in double quotes with attribute classNameJSX uses ... Read More

Standalone React.js basic example

Shyam Hande
Updated on 03-Jul-2020 09:02:34

2K+ Views

Lets first start with writing a simple HTML code and see how we can use ReactBasic React example  − Create a simple div like below −    Steve    My hobby: Cricket Add some styling elements.player{    border:1px solid #eee;    width:200px;    box-shadow:0 2px 2px #ccc;    padding: 22px;    display:inline-block;    margin:10px; }This is just like normal html data in web app. Now, we may have multiple same players and we then have to replicate the same div like below    David    My hobby: Cricket These div are same in structure but having different data inside. Here, ... Read More

Advertisements