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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

useFormikSlice feature that updates when I need #2089

Open
outerlook opened this issue Dec 6, 2019 · 1 comment
Open

useFormikSlice feature that updates when I need #2089

outerlook opened this issue Dec 6, 2019 · 1 comment
Labels

Comments

@outerlook
Copy link

outerlook commented Dec 6, 2019

馃殌 Feature request

Current Behavior

Currently, when I need to access some form's values from deep nested components, if they are many, I use useFormikContext(), but it then gets updated and re rendered on every change to formik context (right?).

If I need to optmize this, I start using many useField()`s which already updates only if the value updates. But then it gets ugly quickly.

Desired Behavior

I would like some function which returns:

  • values (the ones I need)
  • errors (here too)
  • setFieldValue (could this remain typed to work with all fields or should be only the one I need?)
  • setFieldErrors
    ... useful things already present on useField but in a batch way.

Suggested Solution

Sort of useFormikSlice([field1, field2, field3]) maybe?

Types would be like
useFormikSlice< AllMyValues >([field1,field2,field3])
that returns things with Pick< AllMyValues, field1|field2|field3>

Who does this impact? Who is this for?

People aiming to optimize some uses with less code.

Describe alternatives you've considered

should keep things simple and just add an overload to useFormikContext() which accepts one arg "deps"?

Additional context

Sorry if this behaviour already exists somehow and I did not realize 馃榿

@jaredpalmer
Copy link
Owner

jaredpalmer commented Dec 6, 2019

Yeah would be rad, but there isn鈥檛 a way to do this with hooks (only components using scu). This is because there is no way to subscribe to slices of context with hooks right now. To make a hook like this we would have to rewrite formik so that state would live outside of react and in an external pub/sub object.

I鈥檝e considered this multiple times but have punted, hoping that react team would add this functionality to useContext, but I doubt that will happen at this point. Even though this goes against my initial design principles of formik, they were formed before hooks existed, when sCU was viable.

I think it鈥檚 worth experimenting with pub sub, but in the short term it would be rather simple to add <FormikSlice/> and withFormikSlice(). Or we could pass connect() mapFormikStateAndHelpersToProps like fn

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

No branches or pull requests

2 participants