Skip to content

Instantly share code, notes, and snippets.

@ELanning
ELanning / demo.tsx
Created January 25, 2024 08:38
WithState + Functional Children
import { useState } from 'react';
interface WithStateProps {
children: ({ state, setState }: { state: number; setState: (x: number) => void }) => React.ReactNode;
}
export function WithState<P extends object>(Component: React.ComponentType<P>) {
return function WithStateComponent(props: P & WithStateProps) {
const [state, setState] = useState(0);
@ELanning
ELanning / avoidParentAndSiblingIndexImports.js
Created October 18, 2020 01:07
Checks for importing sibling or parent from index.js
// Add to your checkr.js file.
function avoidParentAndSiblingIndexImports({ fileExtension }, underline) {
if (fileExtension !== "js" && fileExtension !== "jsx") {
return;
}
const siblingImport = /from '\.\/';/g;
underline(
siblingImport,
"Avoid importing from sibling modules index.js",
"info"
@ELanning
ELanning / noModifyingUseStateDirectly.js
Created October 18, 2020 01:05
Check for modifying useState variables directly
// Add to your checkr.js file.
function noModifyingUseStateDirectly(
{ fileExtension, fileContents },
underline
) {
if (fileExtension !== "js" && fileExtension !== "jsx") {
return;
}
const useState = /const \[(.+), .+\] = useState\([\s\S]*?\);/g;
let useStateMatches;
@ELanning
ELanning / mismatchedUseStateNames.js
Created October 18, 2020 00:35
Check for mismatched useState names
// Add to your checkr.js file.
function mismatchedUseStateNames({ fileExtension, fileContents }, underline) {
if (fileExtension !== "js" && fileExtension !== "jsx") {
return;
}
const useState = /const \[(.+), (.+)\] = useState\([\s\S]*?\);/g;
let useStateMatches;
while ((useStateMatches = useState.exec(fileContents)) != null) {
const line = useStateMatches[0];