1 April 2020

Component Life Cycle in react js

  • Create Components folder under src folder
  • Add ComponentLifeCycle.js file and add below Code
import React, { Component } from "react";
class ComponentLifeCycle extends Component {
constructor(props) {
super(props);
this.state = { hello: "Adi" };
this.changeState = this.changeState.bind(this);
}
render() {
return (
<div>
Component Life Cycle
<h3>Hello {this.state.hello}</h3>
<button onClick={this.changeState}>Click Here!</button>
</div>
);
}
UNSAFE_componentWillMount() {
console.log("Component Will MOUNT!");
}
componentDidMount() {
console.log("Component Did MOUNT!");
}
changeState() {
this.setState({ hello: "All!!- Its a great reactjs ." });
}
UNSAFE_componentWillReceiveProps(newProps) {
console.log("Component Will Recieve Props!");
}
shouldComponentUpdate(newProps, newState) {
return true;
}
UNSAFE_componentWillUpdate(nextProps, nextState) {
console.log("Component Will UPDATE!");
}
componentDidUpdate(prevProps, prevState) {
console.log("Component Did UPDATE!");
}
componentWillUnmount() {
console.log("Component Will UNMOUNT!");
}
}
export default ComponentLifeCycle;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import ComponentLifeCycle from "./components/ComponentLifeCycle";

function App() {
return (
<div className="App">
<ComponentLifeCycle />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Output:






Complex Conditionally applying CSS classes in react js

  • Create Components folder under src folder
  • Add CSScomplexconditions.js file and add below Code
import React, { Component } from "react";
import "./CSScomplexconditions.css";

class CSScomplexconditions extends Component {
getClass(someInput) {
switch (someInput) {
case "1": {
return "class1";
}
case "2": {
return "class2";
}
case "3": {
return "class3";
}
}
}
render() {
return (
<div>
complex Conditionally applying CSS classes
<p className={this.getClass("2")}>Example Text</p>
</div>
);
}
}
export default CSScomplexconditions;
  • Add CSScomplexconditions.css file and add below Code
.class1 {
background-color: red;
}
.class2 {
background-color: blue;
}
.class3 {
background-color: orangered;
}
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import CSScomplexconditions from "./components/CSScomplexconditions";

function App() {
return (
<div className="App">
<CSScomplexconditions />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:



Conditionally applying inline styles in react js

  • Create Components folder under src folder
  • Add CSSConditionallyinline.js file and add below Code
import React, { Component } from "react";

class CSSConditionallyinline extends Component {
constructor() {
super();
this.state = { isRed: true };
}

render() {
const isRed = this.state.isRed;

return (
<div>
Conditionally applying inline styles
<p style={{ color: isRed ? "red" : "blue" }}>Example Text</p>
</div>
);
}
}
export default CSSConditionallyinline;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import CSSConditionallyinline from "./components/CSSConditionallyinline";

function App() {
return (
<div className="App">
<CSSConditionallyinline />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:






Modal popup in react js

  • Create Components folder under src folder
  • Add ReactModalPopup.js file and add below Code
import React from "react";
import "./ReactModalPopup.css";

class ReactModalPopup extends React.Component {
state = { show: false };

showModal = () => {
this.setState({ show: true });
};

hideModal = () => {
this.setState({ show: false });
};

render() {
return (
<main>
<h1>React Modal</h1>
<Modal show={this.state.show} handleClose={this.hideModal}>
<p>Modal</p>
<p>Data</p>
</Modal>
<button type="button" onClick={this.showModal}>
Open
</button>
</main>
);
}
}
export default ReactModalPopup;

const Modal = ({ handleClose, show, children }) => {
const showHideClassName = show ? "modal display-block" : "modal display-none";

return (
<div className={showHideClassName}>
<section className="modal-main">
{children}
<button onClick={handleClose}>Close</button>
</section>
</div>
);
};
  • Add ReactModalPopup.css file and add below Code
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.6);
}
.modal-main {
position: fixed;
background: white;
width: 80%;
height: auto;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.display-block {
display: block;
}

.display-none {
display: none;
}
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import ReactModalPopup from "./components/ReactModalPopup";

function App() {
return (
<div className="App">
<ReactModalPopup />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Output:









Rendering raw html data in react js

  • Create Components folder under src folder
  • Add RenderingRawHtml.js file and add below Code
import React, { Component } from "react";
import Parser from "html-react-parser";

class RenderingRawHtml extends Component {
constructor(props) {
super(props);
this.state = { htmlData: "<div><h2><u>HTML Data</u></h2></div>" };
}
render() {
return (
<>
<h3>Rendering raw html data in reactjs Example</h3>
<div>{Parser(this.state.htmlData)}</div>
</>
);
}
}
export default RenderingRawHtml;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import RenderingRawHtml from "./components/RenderingRawHtml";

function App() {
return (
<div className="App">
<RenderingRawHtml />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:







Password strength example in react js

  • Create Components folder under src folder
  • Add PasswordStrength.js file and add below Code
import React from "react";

class PasswordStrength extends React.Component {
constructor(props) {
super(props);
this.state = {
password: "",
containsEnoughChars: false,
containsSpecialChars: false,
containsUpperAndLowerChars: false,
isPasswordValid: false
};
this.handleInput = this.handleInput.bind(this);
this.toggleRevealPassword = this.toggleRevealPassword.bind(this);
this.submitPassword = this.submitPassword.bind(this);
}
render() {
return (
<div style={{ padding: "1rem" }}>
<div>
<input
onChange={this.handleInput}
type={this.state.isPasswordRevealed ? "text" : "password"}
value={this.state.password}
/>
<input
type="checkbox"
checked={this.state.isPasswordRevealed}
onChange={this.toggleRevealPassword}
/>
Show Password
</div>

<div>
<div
style={{
backgroundColor: this.state.hasEnoughChars ? "green" : "red"
}}
>
Minimum 8 characters
</div>
<div
style={{
backgroundColor: this.state.hasSpecialChars ? "green" : "red"
}}
>
Minimum 1 special character
</div>
<div
style={{
backgroundColor: this.state.hasUpperAndLowercaseChars
? "green"
: "red"
}}
>
Include 1 uppercase and lowercase letter
</div>
</div>

<button
disabled={!this.state.isPasswordValid}
onClick={this.submitPassword}
>
Submit
</button>
</div>
);
}

handleInput(e) {
this.setState({ password: e.target.value });

this.setState(prevState => ({
hasEnoughChars: prevState.password.length >= 8,
hasUpperAndLowercaseChars:
/[a-z]/.test(prevState.password) && /[A-Z]/.test(prevState.password),
hasSpecialChars: /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/.test(
prevState.password
)
}));

this.setState(prevState => ({
isPasswordValid:
prevState.hasEnoughChars &&
prevState.hasUpperAndLowercaseChars &&
prevState.hasSpecialChars
}));
}

toggleRevealPassword() {
this.setState({ isPasswordRevealed: !this.state.isPasswordRevealed });
}

submitPassword() {
alert(`Password submitted: ${this.state.password}`);
}
}

export default PasswordStrength;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import PasswordStrength from "./components/PasswordStrength";

function App() {
return (
<div className="App">
<PasswordStrength />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Output:






Find all possible combinations of numbers in c#


Code:

using System;
using System.Collections.Generic; namespace All_Possible_Combinations_of_a_number { class Program { static void Main(string[] args) { Console.WriteLine("Enter a Number:"); int number = Convert.ToInt32(Console.ReadLine()); List<int> listNumbers = new List<int>(); for (int i=1;i<=number;i++) { listNumbers.Add(i); } Console.WriteLine("Output:"); double count = Math.Pow(2, listNumbers.Count); for (int i = 1; i <= count - 1; i++) { string aa = ""; for (int j = 0; j < listNumbers.Count; j++) { int b = i & (1 << j); if (b > 0) { aa = aa + listNumbers[j]; } } Console.WriteLine(aa); } Console.ReadLine(); } } }


Output:


Sort numbers based on last digit in c#


Code:

using System; using System.Collections.Generic; using System.Linq; namespace Sort_numbers_based_on_last_digit { class Program { static void Main(string[] args) { List<MyNumbers> listMyNumbers = new List<MyNumbers>(); listMyNumbers.Add(new MyNumbers { Number = 13 }); listMyNumbers.Add(new MyNumbers { Number = 23 }); listMyNumbers.Add(new MyNumbers { Number = 123 }); listMyNumbers.Add(new MyNumbers { Number = 4 }); listMyNumbers.Add(new MyNumbers { Number = 345 }); listMyNumbers.Add(new MyNumbers { Number = 1235 }); listMyNumbers.Add(new MyNumbers { Number = 2345 }); listMyNumbers.Add(new MyNumbers { Number = 12345 }); listMyNumbers.Add(new MyNumbers { Number = 1 }); listMyNumbers.Add(new MyNumbers { Number = 2 }); listMyNumbers.Add(new MyNumbers { Number = 14 }); listMyNumbers.Add(new MyNumbers { Number = 24 }); listMyNumbers.Add(new MyNumbers { Number = 34 }); listMyNumbers.Add(new MyNumbers { Number = 124 }); listMyNumbers.Add(new MyNumbers { Number = 234 }); listMyNumbers.Add(new MyNumbers { Number = 1234 }); listMyNumbers.Add(new MyNumbers { Number = 5 }); listMyNumbers.Add(new MyNumbers { Number = 15 }); listMyNumbers.Add(new MyNumbers { Number = 25 }); listMyNumbers.Add(new MyNumbers { Number = 35 }); listMyNumbers.Add(new MyNumbers { Number = 45 }); listMyNumbers.Add(new MyNumbers { Number = 125 }); listMyNumbers.Add(new MyNumbers { Number = 12 }); listMyNumbers.Add(new MyNumbers { Number = 3 }); listMyNumbers.Add(new MyNumbers { Number = 235 }); List<MyNumbers> listMyNumbers1 = new List<MyNumbers>(); foreach (var a in listMyNumbers) { MyNumbers obj = new MyNumbers(); obj.Number = a.Number; obj.LastNumber= Math.Abs(a.Number) % 10; obj.TotalCount = (a.Number).ToString().Length; listMyNumbers1.Add(obj); } List<MyNumbers> listMyNumbers2 = listMyNumbers1.OrderBy(x => x.LastNumber).ThenBy(x=>x.TotalCount).ToList(); Console.WriteLine("Input:"); foreach(var a in listMyNumbers) { Console.WriteLine(a.Number); } Console.WriteLine("Output:"); foreach (var a in listMyNumbers2) { Console.WriteLine(a.Number); } Console.ReadLine(); } } public class MyNumbers { public int Number { get; set; } public int LastNumber { get; set; } public int TotalCount { get; set; } } }


Input:

Output: