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

Fix: Introduce useFastField React hook #1772

Closed
wants to merge 4 commits into from

Conversation

sparkboom
Copy link

@sparkboom sparkboom commented Aug 25, 2019

This PR offers an implementation of useFastField React hook as a solution for issue #1026 and #671 for Formik's 'next' release. It behaves like a FastField where the state of the field does not update Formik's values and errors model until the user's focus is removed from it providing some optimised behaviour.

Included in this PR is

  1. Suggested document additions for the useFastField hook
  2. Simple hook implementation.

View rendered docs/api/useFastField.md

@johnrom
Copy link
Collaborator

johnrom commented Aug 30, 2019

I wonder if it would be useful to skip the optimization when there is an error, so that the error can be resolved as soon as possible. (A user might think they still haven't fixed the value until the error goes away)

@jaredpalmer
Copy link
Owner

@johnrom can you share some pseudo-code for how that might work?

@johnrom
Copy link
Collaborator

johnrom commented Aug 30, 2019

I might be way overthinking this but:

field.onChange = React.useCallback(
    meta.error 
        // if there's an error, just call onChange
        ? onChange
        // am I delusional? do we need to support onChange('string', value) ??
        : (
            eventOrPath: string | React.ChangeEvent<any>
        ): void | ((eventOrTextValue: string | React.ChangeEvent<any>) => void) => {
            if (isString(eventOrPath)) {
                return event => setValue(event.target.value);
            } else {
                setValue(eventOrPath.target.value);
            }
        },
    [meta.error]
);

@wellyal
Copy link

wellyal commented Sep 9, 2019

@sparkboom nice!! I have a scenario where I have to use a custom onChange function. Wouldn't be nice if the useFastField also received a custom onChange function as a parameter?

@3nvi
Copy link

3nvi commented Sep 18, 2019

While this approach is not bad, I personally think that it doesn't help when you want to react to something as you are typing (before you blur).

I think that Formik might need to combat the underlying issue that every Field component gets updated on every values update, regardless of whether it needed to update or not (related issue #1804)

While useFastField could work in some cases, I'm not fully sure if it's a solution...

@jaredpalmer
Copy link
Owner

@3nvi I think we'll be able to tackle this in v3 with React Flare

@3nvi
Copy link

3nvi commented Oct 25, 2019

@3nvi I think we'll be able to tackle this in v3 with React Flare

That's great! Is there an (unofficial & non-bounding) estimation on that?

@stale stale bot added the stale label Dec 24, 2019
@maxijonson
Copy link

I noticed, in the PR's suggested useFastField, that the type of props is FieldAttributes<Val> instead of string | FieldHookConfig<Val> (type of propsOrFieldName for useField). Is there any reason for that? Also, is there a reason why helpers is not returned like useField?

@stale stale bot removed the stale label Jan 8, 2020
@johnrom
Copy link
Collaborator

johnrom commented Jan 8, 2020

@jaredpalmer just came across your note here and it looks like Flare has been abandoned:

We're not longer working on this particular approach and will be exploring other ways to work with the DOM event system in the future. We've concluded that the "Flare" event system is too high-level an abstraction and we'd like to explore something that is a bit more familiar to developers familiar with the DOM (e.g., addEventListener) and React's existing tools (e.g., hooks). Our goal is still to make it possible for library authors to work with passive events, capture/bubble phase, custom events, and events occurring on the document from within a React function component...while reducing the amount of event-related code needed in ReactDOM. Ultimately building both intermediate abstractions like the Responder Event system and high-level abstractions like useTap (which we prototyped in Flare) should be possible in user-space.

facebook/react#15257

@jaredpalmer
Copy link
Owner

jaredpalmer commented Jan 8, 2020 via email

@chriskn
Copy link

chriskn commented Apr 14, 2020

Hi, @jaredpalmer are there any plans to provide a useFastField hook? The slow performance, specially in IE is a showstopper for me.

@stale stale bot removed the stale label Apr 14, 2020
export function useFastField<Val = any>(
props: FieldAttributes<Val>
): [FieldInputProps<Val>, FieldMetaProps<Val>] {
const [field, meta] = useField<Val>(props);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sparkboom missing helpers right?

@sibelius
Copy link

can we release this as experimental?

@Bluefitdev Bluefitdev mentioned this pull request May 21, 2020
@Dashue
Copy link

Dashue commented May 25, 2020

Another guy here that could benefit from this

@Its-Alex
Copy link

Its-Alex commented Jun 2, 2020

Up?

@sibelius
Copy link

sibelius commented Jun 2, 2020

you can copy and paste from here https://gist.github.com/sibelius/ccea4d505eb20d0dd08c19716c469093

until this lands

@gperez-10pines
Copy link

Looks like approved but somebody is not yet pressing the button?

@johnrom
Copy link
Collaborator

johnrom commented Sep 18, 2020

@gperez-10pines not at all. None of the solutions are ideal, the performance with Field itself needs to be fixed. FastField was a meh solution in v1. You're free to try various userland solutions, but I don't think Formik will adopt anything until we figure out a real highly optimized method using #2749 or something else.

I believe the React team has focused on "abandoning renders" when nothing really changes instead of preventing renders from starting, so has not been focused on optimizing Context which Formik is using and is causing this performance issue. At least that's my impression of the state of things.

@jaredpalmer
Copy link
Owner

This is correct. However, I'm working on fixing field. Fastfield was a hack for v1 that has no parallel in hooks land. shouldComponentUpdate is different that the implementation here. My progress on fixing this can be seen here #2749

@jaredpalmer
Copy link
Owner

This has been implemented in v3 with just the regular useField. Closing.

@ortonomy
Copy link

sorry to dig this up, but @jaredpalmer -- I can't see any reference to a fast boolean or anything else about how this is implemented in regular useField in the docs: https://formik.org/docs/api/useField

Is this just built in behaviour for the hook?

@honia19
Copy link

honia19 commented Sep 13, 2024

sorry to dig this up, but @jaredpalmer -- I can't see any reference to a fast boolean or anything else about how this is implemented in regular useField in the docs: https://formik.org/docs/api/useField

Is this just built in behaviour for the hook?

Formik died.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.