final-form / react-final-form

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

Custom form state and subscribing to it #637

Open nvaken opened 5 years ago

nvaken commented 5 years ago

Are you submitting a bug report or a feature request?

Feature request

What is the current behavior?

I'm unable to add custom form state, which I can subscribe to.

What is the expected behavior?

I'd like to be able to add a form state value with all fieldnames which only changes when the count of fields change. This way, I can subscribe to changes in the amount of fields. I currently am trying to use a custom mutator which adds this to the state and running this mutator from a FormSpy which is subscribed to modified.

                    <FormSpy subscription={{ modified: true }}>
                      {({form}) => {

                        form.mutators.indexFieldnames();
                          return (<p>test</p>)
                      }}
                    </FormSpy>
                    <FormSpy subscription={{ fieldnames: true }}>
                      {props => {
                        return (
                          <p>Only render when fieldnames change</p>
                        )
                      }}
                    </FormSpy>
import { setIn } from "final-form";

/**
 */
const indexFieldnames = ([], state) => {
  state = setIn(state, 'formState.fieldnames', Object.keys(state.fields));
}

export default indexFieldnames;

The above seems to work, I see the state changes when using console.log() on the state in the first FormSpy, though my second formspy never gets executed. I'm suspecting that this is simply not a feature at this point?

Disclaimer: I'm aware that indexFieldnames in this setup would retrigger subscription every time it is run since I'm unconditionally altering the state, but I'll be checking for changes and only then altering the state as soon as the subscription works.

What's your environment?

React final form 6.3.0 Final form 4.18.5

cdomigan commented 3 years ago

@nvaken Did you ever get this to work? I'm interested in similar functionality for tracking disabled & loading states, etc