Wednesday, 27 October 2021

What is React State?

 The state is an updatable structure that is used to contain data or information about the component. The state in a component can change over time. The change in state over time can happen as a response to user action or system event. A component with the state is known as stateful components. 

It is the heart of the react component which determines the behavior of the component and how it will render. They are also responsible for making a component dynamic and interactive.

A state must be kept as simple as possible. It can be set by using the setState() method and calling setState() method triggers UI updates. 

A state represents the component's local state or information. It can only be accessed or modified inside the component or by the component directly. To set an initial state before any interaction occurs, we need to use the getInitialState() method.

For example, if we have five components that need data or information from the state, then we need to create one container component that will keep the state for all of them.

Defining State

To define a state, you have to first declare a default set of values for defining the component's initial state. To do this, add a class constructor which assigns an initial state using this.state. The 'this.state' property can be rendered inside render() method.

Example

The below sample code shows how we can create a stateful component using ES6 syntax.

  1. import React, { Component } from 'react';  
  2. class App extends React.Component {  
  3.  constructor() {  
  4.       super();        
  5.       this.state = { displayBio: true };  
  6.       }  
  7.       render() {  
  8.           const bio = this.state.displayBio ? (  
  9.               <div>  
  10.                   <p><h3>Javatpoint is one of the best Java training institute in Noida, Delhi, Gurugram, Ghaziabad and Faridabad. We have a team of experienced Java developers and trainers from multinational companies to teach our campus students.</h3></p>   
  11.             </div>  
  12.               ) : null;  
  13.               return (  
  14.                   <div>  
  15.                       <h1> Welcome to JavaTpoint!! </h1>  
  16.                       { bio }   
  17.                   </div>  
  18.               );  
  19.      }  
  20. }  
  21. export default App;  

To set the state, it is required to call the super() method in the constructor. It is because this.state is uninitialized before the super() method has been called.

Output

ReactJS State

Changing the State

We can change the component state by using the setState() method and passing a new state object as the argument. Now, create a new method toggleDisplayBio() in the above example and bind this keyword to the toggleDisplayBio() method otherwise we can't access this inside toggleDisplayBio() method.

  1. this.toggleDisplayBio = this.toggleDisplayBio.bind(this);  

Example

In this example, we are going to add a button to the render() method. Clicking on this button triggers the toggleDisplayBio() method which displays the desired output.

  1. import React, { Component } from 'react';  
  2. class App extends React.Component {  
  3.  constructor() {  
  4.       super();        
  5.       this.state = { displayBio: false };  
  6.       console.log('Component this', this);  
  7.       this.toggleDisplayBio = this.toggleDisplayBio.bind(this);  
  8.       }  
  9.       toggleDisplayBio(){  
  10.           this.setState({displayBio: !this.state.displayBio});  
  11.           }  
  12.       render() {  
  13.           return (  
  14.               <div>  
  15.                   <h1>Welcome to JavaTpoint!!</h1>  
  16.                   {  
  17.                       this.state.displayBio ? (   
  18.                           <div>  
  19.                               <p><h4>Javatpoint is one of the best Java training institute in Noida, Delhi, Gurugram, Ghaziabad and Faridabad. We have a team of experienced Java developers and trainers from multinational companies to teach our campus students.</h4></p>  
  20.                               <button onClick={this.toggleDisplayBio}> Show Less </button>  
  21.                         </div>  
  22.                           ) : (  
  23.                               <div>  
  24.                                   <button onClick={this.toggleDisplayBio}> Read More </button>  
  25.                               </div>  
  26.                           )  
  27.                   }  
  28.              </div>  
  29.         )  
  30.     }  
  31. }  
  32. export default App;  

Output:

ReactJS State

When you click the Read More button, you will get the below output, and when you click the Show Less button, you will get the output as shown in the above image.

ReactJS State



Developing Solutions for Microsoft Azure: Implementing IaaS Solutions

 Question(1): Which is an example of serverless compute?

Ans:



Question(2)
:

Which is a group of datacenters in one geographic location?


Ans:



Question(3)
:

How can you access PowerShell for Azure?

Ans: 



Question(4)
:

ARM templates use what type of syntax?


Ans:




Question(5)
:

What format do ARM templates use?

Ans:


Question(6)
:

What is included in a container?


Ans:




Question(7)
:

What is the name of the container image file?


Ans:


Question(8)
:

Azure Container Registry is built on which technology?


Ans:



Question(9)
:

What is the tool used to rebuild images based on upstream code commits?


Ans:

Question(10)
:

Where can container images be sourced from?


Ans:




Tuesday, 26 October 2021

React Material UI Expansion Panel And Dialogs

 In this article, we will learn how to add dialogs boxes and Expansion Panel in React applications. The expansion panel is used to show and hide content. The expansion Panel basically manages a large amount of content and list data. Dialogs are used to show notifications and alert messages.

 
You can check my previous article in which we discussed the basics of Material UI, from the below-mentioned links. 
  • Add Material UI In React Application
  • Add Material-UI Table In ReactJS Application
Prerequisites
  • Basic Knowledge of React
  • Visual Studio Code
  • Node and NPM installed
  • Material UI Installed

Create ReactJS project

 
Let's first create a React application using the following command.
  1. npx create-react-app matform    

Install Material-UI

 
Now Install Material-UI by using the following command.
  1. npm install @material-ui/core --save    
Now install Material icons by using the following command.
  1. npm install @material-ui/icons  
Now, right-click on the "src" folder and add a new component named 'Panel.js
 
First let’s import required material UI component; add the following reference in this component. 
  1. import AppBar from '@material-ui/core/AppBar';  
  2. import Toolbar from '@material-ui/core/Toolbar';  
  3. import ExpansionPanel from '@material-ui/core/ExpansionPanel';  
  4. import ExpansionPanelSummary from '@material-ui/core/ExpansionPanelSummary';  
  5. import ExpansionPanelDetails from '@material-ui/core/ExpansionPanelDetails';  
  6. import ExpandMoreIcon from '@material-ui/icons/ExpandMore';  
Add the following code in panel component.
  1. import React, { Component } from 'react'    
  2. import AppBar from '@material-ui/core/AppBar';    
  3. import Toolbar from '@material-ui/core/Toolbar';    
  4. import ExpansionPanel from '@material-ui/core/ExpansionPanel';    
  5. import ExpansionPanelSummary from '@material-ui/core/ExpansionPanelSummary';    
  6. import ExpansionPanelDetails from '@material-ui/core/ExpansionPanelDetails';    
  7. import ExpandMoreIcon from '@material-ui/icons/ExpandMore';    
  8. export class Panel extends Component {    
  9.         render() {    
  10.                 return (    
  11.                  <div>    
  12.                   <AppBar position="static">    
  13.                    <Toolbar style={{ 'paddingLeft': "600px" }}>    
  14.                     Material UI Expansion Panel    
  15.                     </Toolbar>    
  16.                      </AppBar>    
  17.                       <ExpansionPanel>    
  18.                       <ExpansionPanelSummary expandIcon={<ExpandMoreIcon></ExpandMoreIcon>} >    
  19.                         <p>Delhi</p>    
  20.                         </ExpansionPanelSummary>    
  21.                         <ExpansionPanelDetails>    
  22.                         <p>Delhi, India’s capital territory, is a massive metropolitan area in the country’s north. In Old Delhi, a neighborhood dating to the 1600s, stands the imposing Mughal-era Red Fort, a symbol of India, and the sprawling Jama Masjid mosque, whose courtyard accommodates 25,000 people </p>    
  23.                       </ExpansionPanelDetails>    
  24.                          </ExpansionPanel>    
  25.                           <ExpansionPanel>    
  26.                             <ExpansionPanelSummary expandIcon={<ExpandMoreIcon></ExpandMoreIcon>} >    
  27.                               <p>Bangalore</p>    
  28.                                </ExpansionPanelSummary>    
  29.                                  <ExpansionPanelDetails>    
  30.                                 <p>   Bengaluru (also called Bangalore) is the capital of India's southern Karnataka state. The center of India's high-tech industry, the city is also known for its parks and nightlife. By Cubbon Park, Vidhana Soudha is a Neo-Dravidian legislative building. Former royal residences include 19th-century Bangalore Palace, modeled after England’s Windsor Castle, an 18th-century teak structure </p>    
  31.                                         </ExpansionPanelDetails>    
  32.                                 </ExpansionPanel>    
  33.                                 <ExpansionPanel >    
  34.                                  <ExpansionPanelSummary expandIcon={<ExpandMoreIcon></ExpandMoreIcon>}        >    
  35.                                    <p>Jaipur</p>    
  36.                                  </ExpansionPanelSummary>    
  37.                                 <ExpansionPanelDetails>    
  38.                        <p>  Jaipur is the capital of India’s Rajasthan state. It evokes the royal family that once ruled the region and that, in 1727, founded what is now called the Old City, or “Pink City” for its trademark building color. At the center of its stately street grid (notable in India) stands the opulent, colonnaded City Palace comple </p>    
  39.                                         </ExpansionPanelDetails>    
  40.                                 </ExpansionPanel>    
  41.                         </div>    
  42.                 )    
  43.         }    
  44. }    
  45.     
  46. export default Panel     
Now run the project by using 'npm start'.
 
React Material UI Expansion Panel And Dialogs
 
Dialog Box:Dialogs are used to show notifications and alert messages.
 
Now, right-click on the "src" folder and add another component named 'Dialog.js'. In this component, we add dialog boxes to show important messages and information.
 
Add the following code in Dialogcomponent. 
  1. import React from 'react';  
  2. import Button from '@material-ui/core/Button';  
  3. import Dialog from '@material-ui/core/Dialog';  
  4. import DialogActions from '@material-ui/core/DialogActions';  
  5. import DialogTitle from '@material-ui/core/DialogTitle';  
  6. import AppBar from '@material-ui/core/AppBar';  
  7. import Toolbar from '@material-ui/core/Toolbar';  
  8.   
  9. export default function Dialogs() {  
  10.   const [state, setstate] = React.useState(false);  
  11.   
  12.   const Open = () => {  
  13.     setstate(true);  
  14.   };  
  15.   
  16.   const Close = () => {  
  17.     setstate(false);  
  18.   };  
  19.   
  20.   return (  
  21.     <div>  
  22.       <AppBar className="mrg" position="static">  
  23.        <Toolbar style={{ 'paddingLeft': "600px" }}>  
  24.            Material UI Dialog  
  25.          </Toolbar>  
  26.         </AppBar>  
  27.       <Button variant="contained" color="primary" onClick={Open}>  
  28.         Open Dialog  
  29.       </Button>  
  30.       <Dialog  
  31.         open={state}  
  32.         onClose={Close}  
  33.         aria-labelledby="alert-dialog-title"  
  34.         aria-describedby="alert-dialog-description" >  
  35.         <DialogTitle id="alert-dialog-title">{"Are you Sure to Delete?"}</DialogTitle>  
  36.         <DialogActions>  
  37.           <Button onClick={Close} color="primary">  
  38.             Yes  
  39.           </Button>  
  40.           <Button onClick={Close} color="primary" autoFocus>  
  41.              No  
  42.           </Button>  
  43.         </DialogActions>  
  44.       </Dialog>  
  45.     </div>  
  46.   );  
  47. }  
Now open app.css file and add the following code
  1. .mrg{  
  2.   margin-top: 20px;  
  3.   margin-bottom: 20px;  
  4. }  
  5. .pding  
  6. {  
  7.   padding-left: 500px;  
  8. }  
Now open app.js file and add the following code. 
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import Form from './Form'  
  5. import Dialogs from './Dialog'  
  6.   
  7. function App() {  
  8.   return (  
  9.     <div className="App">  
  10.       <Panel></Panel>  
  11.     </div>  
  12.   );  
  13. }  
  14.   
  15. export default App;  
Now run the project and check the result.
 
React Material UI Expansion Panel And Dialogs
Click on the button,
 
React Material UI Expansion Panel And Dialogs
 

Summary

 
In this article, we learned how we add Expansion Panel and dialogs in Reactjs applications. Expansion Panel is used to show and hide content.