{{ message }}

Instantly share code, notes, and snippets.

# Filippo Rivolta frivolta

Created Apr 12, 2021
jmawd - lastSolution
View thirdSolution.js
 const result = [...new Set([...firstArr, ...secondArr])]
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)
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]) }
Created Apr 12, 2021
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}`)
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] )
Created Sep 4, 2020
createGenericContext.tsx - typing-react-context-v2
View createGenericContext.tsx
 import React from "react"; export const createGenericContext = () => { // Create a context with a generic parameter or undefined const genericContext = React.createContext(undefined); // Check if the value provided to the context is defined or throw an error const useGenericContext = () => { const contextIsDefined = React.useContext(genericContext); if (!contextIsDefined) {
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 ( <>

Sidebar is: {isOpen ? "Open" : "Closed"}

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
Created Sep 4, 2020
useSidebar.tsx - typing-react-context-v2
View useSidebar.tsx
 import { useState, useEffect } from "react"; export type UseSidebar = [ boolean, React.Dispatch> ]; export const useSidebar = (newOpenValue: boolean): UseSidebar => { const [isOpen, setIsOpen] = useState(true);
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 (