question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-form] Dynamic list addition/removal does not change the dirty state of the form

See original GitHub issue

Overview

When combining useDynamicList and useForm, I would expect that calling addItem or removeItem for the dynamic list would result in the form being marked as dirty. Currently, it is only marked dirty if the values within the form fields have been edited.

The linked CodeSandbox from the documentation exhibits this behaviour and would be a good reference: https://codesandbox.io/s/hungry-rubin-exrkz?hidenavigation=1&theme=dark&file=/src/App.tsx:939-943

Load the form in the sandbox and then click the “Remove” button to remove the only card entry in the list. The list is now empty and I would expect the “Save” button to be available (to update the form to no longer have any cards associated). The actual behaviour is that “Save” is still disabled, so there isn’t a built-in way to solve this using the dirty state.

Potential solution

To update the dirty state of the form when these changes occur seems like it might be a little tricky, since it only accepts the fields value at the moment, and doesn’t have any idea about the addItem or removeItem that are returned by useDynamicList.

  1. Right now fields is a FieldDictionary<Item>[] type, but it could be modified to an object type that extends the array type and also exposes a dirty field that could be set when addItem or removeItem are invoked.
  2. Alternatively, the useDynamicList result could also return a dirty property of its own that could be checked in addition to the dirty property returned by the useForm.
  3. Alternatively, useForm could accept a new argument like “wrap” that would take an object of name->function and then return a wrapped version of each of them, and internally would set the dirty flag if any of those methods are called. This seems like it might be a bit painful to type, but from a developer flexibility standpoint I kinda like it.

Current workaround

Currently to workaround this, I have to keep some additional state in my component that manually wraps addItem and removeItem and keeps state on whether or not they have been called. Looks like this:

import {useState} from 'react';
import {useForm, useDynamicList} from '@shopify/react-form';

function Component() {
  const [listDirty, setListDirty] = useState(false);
  const {fields, addItem, removeItem} = useDynamicList(
    [{name: 'Ryan'}],
    () => ({name: ''})
  );
  const {dirty} = useForm({
    fields: {fields},
    onSubmit: async () => {}
  });
  const add = () => {
    addItem();
    setListDirty(true);
  };
  const remove = (index: number) => {
    removeItem(index);
    setListDirty();
  };
  if (dirty || listDirty) { 
    ...
  } else {
    ...
  }
}

Consuming repo

https://github.com/shopify/services-db

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
oluwatimiocommented, Apr 21, 2021
1reaction
oluwatimiocommented, Apr 21, 2021

We recently patched this in this PR . There’s also some new documentation on initializing the dynamic list here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically adding or removing items within a react-hook ...
Yesterday, as I was trying to implement this, I found a solution that mix and matches react-hook-forms and react-hooks to manage state and...
Read more >
Add and Remove Form fields dynamically with React and ...
Add and Remove Form fields dynamically with React and React Hooks ; handleChange: This function is used to set input field values based...
Read more >
useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and have the options to validate and update the form...
Read more >
Using Forms in React - Dave Ceddia
And if later you choose to add a form library, you'll know how they work ... There's no state in here yet, and...
Read more >
Managing Nested and Dynamic Forms in Angular - Bitovi
Learn how to use Angular's FormArray class to dynamically add and remove FormGroups and FormControls in reactive forms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found