Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render optimization #2749

Merged
merged 10 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/formik/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"react-fast-compare": "^2.0.1",
"scheduler": "^0.18.0",
"tiny-warning": "^1.0.2",
"tslib": "^1.10.0"
"tslib": "^1.10.0",
"use-context-selector": "^1.1.2"
},
"resolutions": {
"@types/react": "16.9.17",
Expand Down
21 changes: 18 additions & 3 deletions packages/formik/src/FormikContext.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import * as React from 'react';
import { FormikContextType } from './types';
import invariant from 'tiny-warning';
import { createContext, useContextSelector } from 'use-context-selector';

export const FormikContext = React.createContext<FormikContextType<any>>(
export const FormikContext = createContext<FormikContextType<any>>(
undefined as any
);
export const FormikProvider = FormikContext.Provider;
export const FormikConsumer = FormikContext.Consumer;

export function FormikConsumer<Values = any>({
children,
}: {
children: (formik: FormikContextType<Values>) => React.ReactNode;
}) {
const formik = useContextSelector(
FormikContext,
React.useCallback(c => c, [])
) as FormikContextType<Values>;
return <>{children(formik)}</>;
}

export function useFormikContext<Values>() {
const formik = React.useContext<FormikContextType<Values>>(FormikContext);
const formik = useContextSelector(
FormikContext,
React.useCallback(c => c, [])
) as FormikContextType<Values>;

invariant(
!!formik,
Expand Down
15 changes: 7 additions & 8 deletions packages/formik/src/connect.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';

import { FormikContextType } from './types';
import { FormikConsumer } from './FormikContext';
import * as React from 'react';
import invariant from 'tiny-warning';
import { FormikConsumer } from './FormikContext';
import { FormikContextType } from './types';

/**
* Connect any component to Formik context, and inject as a prop called `formik`;
Expand All @@ -12,16 +11,16 @@ import invariant from 'tiny-warning';
export function connect<OuterProps, Values = {}>(
Comp: React.ComponentType<OuterProps & { formik: FormikContextType<Values> }>
) {
const C: React.SFC<OuterProps> = (props: OuterProps) => (
<FormikConsumer>
{formik => {
const C: React.FC<OuterProps> = (props: OuterProps) => (
<FormikConsumer
children={(formik: FormikContextType<Values>) => {
invariant(
!!formik,
`Formik context is undefined, please verify you are rendering <Form>, <Field>, <FastField>, <FieldArray>, or your custom context-using component as a child of a <Formik> component. Component name: ${Comp.name}`
);
return <Comp {...props} formik={formik} />;
}}
</FormikConsumer>
/>
);
const componentDisplayName =
Comp.displayName ||
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12770,6 +12770,11 @@ url@^0.11.0:
punycode "1.3.2"
querystring "0.2.0"

use-context-selector@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/use-context-selector/-/use-context-selector-1.1.2.tgz#63bf6e3502fd383f1257c49df2495e05564d262e"
integrity sha512-uwtrMKEHjZQ8JXMZLUljVDY4CqpAC3MgMcmtpZACG+pJeJ7GNDzBW0thC2RbJX2wKYMvLtYRXgQEqaOnmlF/KQ==

use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
Expand Down