-
Notifications
You must be signed in to change notification settings - Fork 22
Code Style Guide
Todd Schiller edited this page Oct 13, 2021
·
16 revisions
Part of the Getting started guide.
This page is for capturing coding style guidelines that aren't automatically enforced by eslint
- Eliminates the risk of prototype pollution.
- See the detect-object-sink rule
This helps visually distinguish cross-module constants from local module variables
Bad:
const sidebarId = "sidebar";
Good:
const SIDEBAR_ID = "sidebar";
Bad:
const MAX_WAIT = 1000;
Good:
const MAX_WAIT_MS = 1000;
If you don't provide a type, Redux Toolkit assumes any
Bad:
removeExtension(
state,
{ payload }
) {
Good:
removeExtension(
state,
{ payload }: PayloadAction<{ extensionId: UUID }>
) {
- Pass a type parameter when using
useField
. The default isany
do you don't get any type checking if you don't specify a type parameter - Do not use the
helpers
fromuseField
. Consider using setFieldValue instead. https://github.com/formium/formik/issues/2268
Bad:
// `helpers` changes on every render :(
const [field, , helpers] = useField<boolean>();
Good:
const [field] = useField<boolean>();
const { setFieldValue } = useFormikContext();
Do not pass an initialState
unless you destructure and handle the error state.
Bad:
// The caller is oblivious to the error
const [value] = useAsyncState(async () => { throw Error() }, [], []);
Good:
const [value, isPending, error] = useAsyncState(async () => { throw Error() }, [], []);
// The caller is forced to deal with undefined
const [value] = useAsyncState(async () => { throw Error() }, []);