Skip to content

Instantly share code, notes, and snippets.

View KevinVR's full-sized avatar
💻
Always Coding

Kevin Van Ryckegem KevinVR

💻
Always Coding
View GitHub Profile
import React, {useState, useMemo} from 'react';
// Define an arrayValue prop
const UseMemoComponent = ({arrayValue}) => {
// Create an object with the arrayValue prop
const myArray = useMemo(() => {
// If the value is undefined, default to 1
const myValue = arrayValue || 1;
import React, {useState, useCallback} from 'react';
// Define an arrayValue prop
const UseCallbackComponent = ({arrayValue}) => {
// Memoize callback with useCallback
// Ensure the reference is always the same as long as the function doesn't change
// So children components do not re-render due to a redundant variable reference change
// Since functions are pass by reference
const createArray = useCallback(() => {
import React, {useState, useMemo} from 'react';
// Import the useSelector hook from the react-redux package
import { useSelector } from 'react-redux'
const UseSelectorComponent = ({arrayValue}) => {
// Retrieve myArray from the Redux store
const myArray = useSelector(state => state.myArray);
return <>
const moment = require('moment');
const databaseDate = '06/01/2021 9:59:01 PM GMT+2';
const jsDate = moment(databaseDate);
console.log(jsDate);
// > Tue Jun 01 2021 21:59:01 GMT+0200
// Calculate time difference from now
console.log(jsDate.fromNow());
const moment = require('moment');
const databaseDate = '06/01/2021 9:59:01 PM GMT+2';
var jsDate = moment(databaseDate, "DD/MM/YYYY HH:mm:ss");
console.log(jsDate);
// > Wed Jan 06 2021 09:59:01 GMT+0100 (CET)
// Calculate time difference from now
console.log(jsDate.fromNow());
new Date().getTime()
< 1614547103822
new Date(1614547103822)
// < Sun Feb 28 2021 22:18:23 GMT+0100 (CET)
import java.util.ArrayList;
// Let's develop our first solution
public class CountingDuplicates {
public static int duplicateCount(String text) {
// We need to count the amount of characters which have duplicates (not the amount of duplicates)
int totalCharsWithDupes = 0;
// Keep an array of characters which have already been counted towards the totalDupes counter
// Since we want to count the amount of characters which have duplicates
import java.util.HashMap;
import java.util.Map;
public class CountingDuplicates {
public static int duplicateCount(String text) {
// Keep our a list with the number of duplicates per character
// HashMap<Character, NumberOfDuplicates>
HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
// Let's only loop once over the full input
<script>
class MyButton extends HTMLElement {
constructor() {
super();
}
}
customElements.define('my-button', MyButton);
</script>
<my-button></my-button>