Skip to content

Instantly share code, notes, and snippets.

View zzdjk6's full-sized avatar

Thor(Shenghan) Chen zzdjk6

  • Palmerston North, New Zealand
View GitHub Profile
@zzdjk6
zzdjk6 / checkFlexGapSupported.ts
Created February 7, 2022 10:02
Detect if flex gap is supported
let _detectedFlexGapSupported = false;
let _isFlexGapSupported = false;
export const checkFlexGapSupported = (): boolean => {
if (_detectedFlexGapSupported) {
return _isFlexGapSupported;
}
// create flex container with row-gap set
const flex = document.createElement("div");
@zzdjk6
zzdjk6 / example.tsx
Last active May 17, 2020 05:15
Data Guard Compose #EarlyReturn
const MyComponent: React.FC = () => {
return (
<Data1Guard>
{(data1) => (
<Data2Guard data1={data1}>
{(data2) => (
<AggregateDataGuard data1={data1} data2={data2}>
{(result) => <ResultDisplay result={result} />}
</AggregateDataGuard>
)}
@zzdjk6
zzdjk6 / example.tsx
Created May 17, 2020 05:10
Data Guard #EarlyReturn
type Data1GuardProps = {
children: (data1: Data1) => JSX.Element;
};
const Data1Guard: React.FC<Data1GuardProps> = ({ children }) => {
const data1 = useData1();
if (!data1) {
return null;
}
return children(data1);
@zzdjk6
zzdjk6 / example.tsx
Created May 17, 2020 05:05
React Hooks Solution #EarlyReturn
const MyComponent: React.FC = () => {
const data1 = useData1();
const data2 = useData2(data1);
const result = React.useMemo(() => {
if (!data1 || !data2) {
return null;
}
return aggregateData(data1, data2);
}, [data1, data2]);
@zzdjk6
zzdjk6 / example.tsx
Last active May 17, 2020 06:49
React Hooks Invalid #EarlyReturn
const MyComponent: React.FC = () => {
const data1 = useData1();
if (!data1) {
return null;
}
const data2 = useData2(data1);
if (!data2) {
return null;
}
@zzdjk6
zzdjk6 / example.tsx
Created May 17, 2020 04:48
React Hooks #EarlyReturn
const MyComponent: React.FC = () => {
const { loading, error, data } = useData();
if (loading) {
return <Spinner />;
}
if (error) {
return <Alert>{error.message}</Alert>;
}
@zzdjk6
zzdjk6 / example.tsx
Created May 17, 2020 04:45
React Component #EarlyReturn
type Props = {
loading: boolean;
error?: Error;
data: Data;
};
const MyComponent: React.FC<Props> = (props) => {
if (props.loading) {
return <Spinner />;
}
@zzdjk6
zzdjk6 / example.js
Last active May 18, 2020 00:50
More Readable #EarlyReturn
function doSomething(user, data) {
if (!hasPermission(user)) {
throw new PermissionDeniedError();
}
if (!isNetworkAvailable()) {
saveInQueue(user, data);
return;
}
@zzdjk6
zzdjk6 / example.js
Last active May 17, 2020 03:48
Less Readable #EarlyReturn
function doSomething(user, data) {
if (hasPermission(user)) {
if (isNetworkAvailable()) {
if (isValid(data)) {
sendToServer(user, data);
} else {
throw new DataInvalidError();
}
} else {
saveInQueue(user, data);
// Use a polyfill to make it useable in old browswers
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';
// When abort happens, this error will be throw
export class AbortError extends Error {
constructor(message: string = 'Aborted') {
super(message);
this.name = 'AbortError';
}
}