final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.38k stars 479 forks source link

Could we have a way to access field data from outside the field? #169

Closed nat-n closed 1 year ago

nat-n commented 6 years ago

Arbitrary state can be stored against a field in field data with a mutator. However it would be useful if this field state could be accessed by other fields, not just within a Field component specifically linked to that particular field.

The only way to do this currently seems to be by executing (and returning the data) from a mutator, but this has the side effect of triggering validation etc whenever called.

My use case is some advanced validations that require consideration of additional field attributes, for which the field data seems like a convenient storage option. However I'd also like to be able to access the validation relevant state from the validation logic of other fields, during their validation without producing a stack overflow from validation triggering validation.

nat-n commented 6 years ago

I've given this a little more thought and I wonder if it would fit the design philosophy for (react-)final-form to support a "selectors" API alongside the mutators API which is exactly the same except without the mutation capability and doesn't trigger side-effects/updates?

Maybe this would detract from the design of subscribers? Though I think there are use-cases for a pull model to complement the push model of subscribers, which could support a more comprehensive view on the state of the whole form without adding much complexity.

gertdreyer commented 1 year ago

Please see: https://final-form.org/docs/final-form/types/FormApi#getstate