Skip to content

Instantly share code, notes, and snippets.

@russellr922
russellr922 / useMergeState.ts
Created April 4, 2020 10:45
React setState hook typescript (merge)
function isSimpleObject(value: any): boolean {
return !!value && value.constructor === Object;
}
function useMergeState<T>(initialState?: T) {
const [mergeState, setMergeState] = isSimpleObject(initialState)
? React.useState<T>(initialState as T)
: React.useState<Partial<T>>();
const setState = React.useCallback((newState: Partial<T>) => {
@russellr922
russellr922 / react-router.types.d.ts
Created January 9, 2020 00:08
React-Router TypeScript RouteComponentProps without importing throughout entire app.
// Reference "Router.RouteComponentProps" in any file without having to import.
// i.e. const MyReactComponent: React.FC<Router.RouteComponentProps> = (props) => (<div>{props.location.pathname}</div>);
// The code below goes into a file named "react-router.types.d.ts" this should go in the same directory as "react-app.env.d.ts" or your global definitions.
import { RouteComponentProps as IRouteComponentProps } from "react-router-dom";
export interface RouteComponentProps<T = {}> extends IRouteComponentProps<T> {};
export as namespace Router;
// Mixin like functionality
const textInput = props => `
color: ${props.error ? color.white : color.base};
background-color: ${props.error ? color.alert : color.white};
`;
export const Input = styled.input`
${textInput}
`;