Skip to content

Instantly share code, notes, and snippets.

View Codennnn's full-sized avatar

LeoKu Codennnn

View GitHub Profile
@Codennnn
Codennnn / 00-README-NEXT-SPA.md
Created March 23, 2023 07:14 — forked from gaearon/00-README-NEXT-SPA.md
Next.js SPA example with dynamic client-only routing and static hosting

Next.js client-only SPA example

Made this example to show how to use Next.js router for a 100% SPA (no JS server) app.

You use Next.js router like normally, but don't define getStaticProps and such. Instead you do client-only fetching with swr, react-query, or similar methods.

You can generate HTML fallback for the page if there's something meaningful to show before you "know" the params. (Remember, HTML is static, so it can't respond to dynamic query. But it can be different per route.)

Don't like Next? Here's how to do the same in Gatsby.

@Codennnn
Codennnn / LoadingBar.tsx
Created November 16, 2022 03:37
react loading bar
interface LoadingBarProps {
className?: string
loading?: boolean
}
export default function LoadingBar(props: LoadingBarProps) {
return (
<div className={`relative min-h-[0.25rem] ${props.className ?? ''}`}>
<div
className={`absolute inset-x-0 bottom-0 z-20 h-1 overflow-hidden py-[1px] ${
@Codennnn
Codennnn / proxy.md
Last active September 28, 2022 06:29
对 GitHub 进行 socket5 代理,提高访问速度。
# 以代理端口 7890 为例
git config --global http.https://github.com.proxy socks5h://127.0.0.1:7890

# 取消代理
git config --global --unset http.https://github.com.proxy
@Codennnn
Codennnn / useEvent.ts
Last active March 16, 2023 02:18
对目标函数进行 memoized,并保证每次调用目标函数时都是最新的。
import { useRef } from 'react';
type AnyFunction = (...args: any[]) => any;
/**
* 对目标函数进行 memoized,并保证每次调用目标函数时都是最新的。
*
* 当你需要把函数加入诸如 `useEffect` 的 dependencyList,而又不希望函数的 update/recreate 造成 hook 执行,就需要用到此方法。
*/
export function useEvent<T extends AnyFunction | undefined>(callback: T): T {
/**
* RuntimeGlobalsChecker
*
* You can use this utility to quickly check what variables have been added (or
* leaked) to the global window object at runtime (by JavaScript code).
* By running this code, the globals checker itself is attached as a singleton
* to the window object as "__runtimeGlobalsChecker__".
* You can check the runtime globals programmatically at any time by invoking
* "window.__runtimeGlobalsChecker__.getRuntimeGlobals()".
*
@Codennnn
Codennnn / useJitRef.ts
Last active May 30, 2022 03:47
两种比较好的方式让 useRef 按需初始值,适用于有条件地调用 ref 或者生成初始值的花销很大的情景。代码来源:https://blog.thoughtspile.tech/2021/11/30/lazy-useref/。
const none = Symbol('lazyRef.Uninitialized')
export default useJitRef(init) {
const value = useRef(none)
const ref = useLazyRef(() => ({
get current() {
if (value.current === none) {
value.current = init()
}
return value.current
/**
* 创建表单 shouldUpdate 函数的简易工具方法
*
* @param namePath - 要响应变化的表单字段,路径格式与调用 Form.getFieldValue 方法的传参一致
*/
export function shouldFormItemUpdate<Values>(
namePath: string | (string | number)[]
): (...value: [Values, Values]) => boolean {
const shouldUpdate = (...value: [Values, Values]) => {
const [prevValue, curValue] = value;
export function highlightJSON(json: string): string {
if (!json) {
return ''
}
if (typeof json !== 'string') {
json = JSON.stringify(json, undefined, 2)
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
import type { FormInstance } from 'antd';
import { Button, Form, Input, Popconfirm, Table } from 'antd';
import React, { useContext, useEffect, useRef, useState } from 'react';
const EditableContext = React.createContext<FormInstance | null>(null);
const EditableRow = ({ index, ...props }: { index: number; [x: string]: any }) => {
const [form] = Form.useForm();
return (
@Codennnn
Codennnn / XxxContext.tsx
Last active March 19, 2022 11:41
react context 的数据状态管理
import { createContext, useContext, useEffect, useState } from 'react';
const XxxContext = createContext({});
export function XxxContextProvider(props: { children: React.ReactNode }) {
const [state, setState] = useState();
const value = { state, setState };
return <XxxContext.Provider value={value}>{props.children}</XxxContext.Provider>;