final-form / final-form/react-final-form
Custom form state and subscribing to it
- Dominant language
- JavaScript
- Stars
- 7.4k
- Forks
- 497
- PR merge metrics
- No merged PRs in 30d
Description
### 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`.
```
{({form}) => {
form.mutators.indexFieldnames();
return (
test
)}}
{props => {
return (
Only render when fieldnames change
)
}}
```
```
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
Contributor guide
Assessment
This issue has not been assessed yet.