Skip to content

Instantly share code, notes, and snippets.

Avatar

Filippo Rivolta frivolta

View GitHub Profile
@frivolta
frivolta / thirdSolution.js
Created Apr 12, 2021
jmawd - lastSolution
View thirdSolution.js
const result = [...new Set([...firstArr, ...secondArr])]
@frivolta
frivolta / secondSolution.js
Created Apr 12, 2021
jmawd - second solution
View secondSolution.js
const concatArr = firstArr.concat(secondArr)
const result = concatArr.filter((item, idx) => concatArr.indexOf(item) === idx)
@frivolta
frivolta / firstSolution.js
Created Apr 12, 2021
jmawd - first solution
View firstSolution.js
const result = []
for (let i = 0; i < firstArr.length; i++) {
if (result.indexOf(firstArr[i]) == -1) result.push(firstArr[i])
}
for (let i = 0; i < secondArr.length; i++) {
if (result.indexOf(secondArr[i]) == -1) result.push(secondArr[i])
}
View arrays.js
const firstArr = new Array(200).fill(undefined).map((val, i) => `item${i}`)
const secondArr = new Array(250).fill(undefined).map((val, i) => `item${i}`)
@frivolta
frivolta / initial.js
Created Apr 12, 2021
jmawd - initial
View initial.js
const firstArr = new Array(200).fill(undefined).map((val, i) => `item${i}`)
const secondArr = new Array(250).fill(undefined).map((val, i) => `item${i}`)
const result = secondArr.reduce(
(acc, item) => {
return acc.includes(item) ? acc : [...acc, item]
},
[...firstArr]
)
@frivolta
frivolta / createGenericContext.tsx
Created Sep 4, 2020
createGenericContext.tsx - typing-react-context-v2
View createGenericContext.tsx
import React from "react";
export const createGenericContext = <T extends unknown>() => {
// Create a context with a generic parameter or undefined
const genericContext = React.createContext<T | undefined>(undefined);
// Check if the value provided to the context is defined or throw an error
const useGenericContext = () => {
const contextIsDefined = React.useContext(genericContext);
if (!contextIsDefined) {
@frivolta
frivolta / ChildComponent.tsx
Created Sep 4, 2020
useSidebarContext.tsx - typing-react-context-v1-v2
View ChildComponent.tsx
import React from "react";
import { useSidebarContext } from "./useSidebarContext";
export const ChildComponent: React.FC = () => {
const [isOpen, setIsOpen] = useSidebarContext();
return (
<>
<p>Sidebar is: {isOpen ? "Open" : "Closed"}</p>
<button onClick={() => setIsOpen(!isOpen)}>Toggle sidebar</button>
@frivolta
frivolta / useSidebarContext.tsx
Created Sep 4, 2020
useSidebarContext.tsx - typing-react-context-v2
View useSidebarContext.tsx
import * as React from "react";
import { useSidebar, UseSidebar } from "./useSidebar";
import { createGenericContext } from "./createGenericContext";
interface Props {
children: React.ReactNode;
}
// Generate context
const [useSidebarContext, SidebarContextProvider] = createGenericContext<
UseSidebar
@frivolta
frivolta / useSidebar.tsx
Created Sep 4, 2020
useSidebar.tsx - typing-react-context-v2
View useSidebar.tsx
import { useState, useEffect } from "react";
export type UseSidebar = [
boolean,
React.Dispatch<React.SetStateAction<boolean>>
];
export const useSidebar = (newOpenValue: boolean): UseSidebar => {
const [isOpen, setIsOpen] = useState(true);
@frivolta
frivolta / index.tsx
Last active Sep 4, 2020
index.tsx - typing-react-context-v2
View index.tsx
import * as React from "react";
import { render } from "react-dom";
import { SidebarProvider, useSidebarContext } from "./useSidebarContext";
import { ChildComponent } from "./childComponent";
export const App = () => {
const [isOpen] = useSidebarContext();
return (
<div>