Skip to content

Instantly share code, notes, and snippets.

@lpolito
lpolito / pr-comments.md
Last active September 24, 2019 16:22 — forked from erickrawczyk/pr-comments.md
PR comments with a purpose

This is a set of ‘statuses’ that pull request comments must be marked with to help explain intent.

  • Required: This must be fixed before merge.
  • Nice to have: This should be fixed eventually.
  • Personal Preference: I would do this, but you don’t have to.
  • Question: What does this do?

Modified from Scaling Square Register on objc.io

@lpolito
lpolito / use-form-state.js
Last active August 27, 2019 19:13
useFormState hook
import React from 'react';
import PropTypes from 'prop-types';
import {useOnUpdateEffect} from 'hooks/use-on-update-effect.mjs';
// Validate form field value simply exists.
export const validateExists = (error) => (value) => (!value ? error : null);
export const formItemPropType = PropTypes.shape({
import React from 'react';
export const useOnUpdateEffect = (callback, dependencies) => {
const isInitRef = React.useRef(false);
React.useEffect(() => {
if (!isInitRef.current) {
isInitRef.current = true;
return;
}
import React from 'react';
export const useComplexState = (initialState) => {
const initialStateRef = React.useRef(null);
if (initialStateRef.current === null) {
// Only get initialState once. This is a sort of memoization of initalState functions.
initialStateRef.current = typeof initialState === 'function' ? initialState() : initialState;
}
// useReducer returns [state, dispatch].
import React from 'react';
import PropTypes from 'prop-types';
import isEqual from 'lodash/isEqual.js';
import {useComplexState} from 'hooks/use-complex-state.mjs';
import {useOnUpdateEffect} from 'hooks/use-on-update-effect.mjs';
// FormItems contexts
const FormItemsContext = React.createContext();
import React from 'react';
// Initialize a ref once during init to prevent something being redefined on every render.
export const useLazyInit = (value) => {
const valueRef = React.useRef(null);
if (valueRef.current === null) {
valueRef.current = typeof value === 'function' ? value() : value;
}