Skip to content

Instantly share code, notes, and snippets.

View hawx1993's full-sized avatar
💭
I may be slow to respond.

trigkit4 hawx1993

💭
I may be slow to respond.
View GitHub Profile
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value; //assign the value of ref to the argument
}, [value]); //this code will run when the value of 'value' changes
return ref.current || {}; //in the end, return the current ref value.
}
export { usePrevious };
var arr1 = [
'common', 'testma',
'xchaos', 'xcrab',
'xfiregod', 'xsea',
'xshelter', 'zeus'
];
var arr2 = [
'common', 'xsea',
'testma', 'xchaos',
'xcrab', 'xshelter',
const glob = require('glob');
const fs = require('fs-extra');
const HeavenIcons = require('@perfma/icons');
const filePath = './src/pages/License/components/StepArea.tsx'; // process.argv[2];
// Usage
// node index.js ../foo/\*.js
// console.log('HeavenIcons', HeavenIcons);
glob(filePath, (err, files) => {
import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react';
type Callback<T> = (value?: T) => void;
type DispatchWithCallback<T> = (value: T, callback?: Callback<T>) => void;
function useStateCallback<T>(initialState: T | (() => T)): [T, DispatchWithCallback<SetStateAction<T>>] {
const [state, _setState] = useState(initialState);
const callbackRef = useRef<Callback<T>>();
const isFirstCallbackCall = useRef<boolean>(true);
import { useCallback, useState } from 'react'
export type MapOrEntries<K, V> = Map<K, V> | [K, V][]
// Public interface
export interface Actions<K, V> {
set: (key: K, value: V) => void
setAll: (entries: MapOrEntries<K, V>) => void
remove: (key: K) => void
reset: Map<K, V>['clear']
import { useCallback, useEffect, useRef } from 'react'
function useIsMounted() {
const isMounted = useRef(false)
useEffect(() => {
isMounted.current = true
return () => {
isMounted.current = false
// 数组去重对象
var arr = [
{ id: 1, city: "南京" },
{ id: 2, city: "南京" },
{ id: 3, city: "杭州" },
{ id: 4, city: "广州" },
];
var newArr = arr.reduce((prev, cur) => {
obj[cur.city] ? "" : (obj[cur.city] = true && prev.push(cur));
@hawx1993
hawx1993 / app.ts
Created March 25, 2020 14:00
app.ts
const SUBSTRING_TO_FIND = '-precache-';
export default async function deleteWorkboxPreCaches(
filename: string,
substringToFind: string = SUBSTRING_TO_FIND,
) {
const cacheNames = await caches.keys();
const cacheNamesToDelete = cacheNames.filter(cacheName => {
return cacheName.includes(substringToFind);
@hawx1993
hawx1993 / buildContainer.ts
Created March 11, 2020 12:59
buildContainer
/*
* @Author: trigkit4
* @Date: 2018-09-18 10:07:05
*/
import React, { Component, ComponentType } from 'react';
import { computed, action, observable } from 'mobx';
import { observer } from 'mobx-react';
import _ from 'lodash';
type BuildContainerOptions<T> = {
@hawx1993
hawx1993 / plain-object.js
Created October 9, 2017 02:39
plain JavaScript objects
var _toString = Object.prototype.toString;
/**
* Strict object type check. Only returns true
* for plain JavaScript objects.
*/
function isPlainObject (obj) {
return _toString.call(obj) === '[object Object]'
}