Skip to content

Instantly share code, notes, and snippets.

@night-fury-rider
night-fury-rider / use-effect.jsx
Last active March 23, 2021 03:18
React Hook - useEffect
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes
@night-fury-rider
night-fury-rider / use-state.tsx
Last active March 23, 2021 03:18
React Hook - useState
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
// count is state variable
// setCount is method to update the count.
// 0 is the default value of count
return (
<button onClick={() => setCount(count + 1)}> Click me </button>
@night-fury-rider
night-fury-rider / react-functional-component.tsx
Last active March 15, 2021 02:08
React Functional component
import React from 'react';
function UVHeader(props: any) {
return (
<div className="uv-header-container">
<span>{props.data.title}</span>
</div>
);
}
@night-fury-rider
night-fury-rider / design-pattern-factory.js
Created September 9, 2020 11:24
Design Pattern - Factory
var Mobile = function(size) {
this.displaySize = size;
};
var Tablet = function(size) {
this.displaySize = size;
};
var Desktop = function(size) {
this.displaySize = size;
@night-fury-rider
night-fury-rider / design-pattern-enhanced-module.js
Last active September 17, 2020 04:06
Design Pattern - Enhanced Module
var Person = function(personName) {
var age = 30; // age will not be accessible outside of Person block
var name = 'Yuvraj';
function getName() {
return name;
}
// This return code block is more clean than that of module pattern
return {
@night-fury-rider
night-fury-rider / design-pattern-module.js
Last active September 17, 2020 04:06
Design Pattern - Module
var Person = function(personName) {
var age = 30; // age will not be accessible outside of Person block
var name = 'Yuvraj';
return {
getName: function() {
return name;
}
}
};
@night-fury-rider
night-fury-rider / design-pattern-prototype.js
Created September 9, 2020 10:48
Design Pattern - Prototype
var Cricketer = function(name, runs) {
this.name = name;
this.runs = runs;
};
Cricketer.prototype.getPlayerDetails = function() {
return this.name + ' has scored ' + this.runs + ' runs';
}
var player1 = new Cricketer('Sachin', 18426);
@night-fury-rider
night-fury-rider / design-pattern-constructor.js
Last active September 9, 2020 10:20
Design Pattern - Constructor
var Person = function(personName) {
this.name = personName;
};
var person1 = new Person('Yuvraj');
var person2 = new Person ('Arya');
console.log(person1.name); // Yuvraj
console.log(person2.name); // Arya
@night-fury-rider
night-fury-rider / design-pattern-singletone.js
Last active September 9, 2020 09:36
Design Pattern - Singletone
var TaskManager = (function(){
var taskManager = null;
function createInstance(id) {
if(taskManager === null) {
taskManager = new Object();
taskManager.id = id;
return taskManager;
}
return taskManager;
@night-fury-rider
night-fury-rider / shot-hand.js
Created September 9, 2020 04:01
Without Short hand condition
if(isVisible) {
console.log('We should use the braces even if there is only one statement in block');
}