Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

[react-form] Use of useChoiceField results in Cannot read property 'dirty' of undefined #1261

Closed
chaddjohnson opened this issue Feb 5, 2020 · 9 comments
Assignees
Labels
Package: react-form stale Stale issue that hasn't received any attention in a while Type: Bug 🐛 Something isn't working

Comments

@chaddjohnson
Copy link

chaddjohnson commented Feb 5, 2020

Please note I do not have the ability to assign the "bug" label to this issue.

Overview

When using useChoiceField, I get an error, Cannot read property 'dirty' of undefined. Here is a code snippet:

import { useForm, useField, useChoiceField, notEmpty } from '@shopify/react-form';

const [product, setProduct] = useState(props.product);

const { fields, submit, submitting, dirty, reset } = useForm({
  fields: {
    name: useField({
      value: product.name,
      validates: [notEmpty("Name can't be blank")]
    }),
    showNotification: useChoiceField({
      value: product.showNotification,
      validates: []
    })
  },
  onSubmit: async form => {
    // ...
  }
});

I also tried

useChoiceField(product.showNotification);

The error definitely occurs when using useChoiceField.

Here is my checkbox (the error occurs whether or not this checkbox is in place):

<Checkbox label={`Show notification`} {...fields.showNotification} />

Consuming repo

What repo were you working in when this issue occurred?

Building my own app using Shopify Polaris.

@chaddjohnson
Copy link
Author

@patsissons @TheMallen @michaelmelanson Any chance we can get this looked at? useChoiceField is broken.

@chaddjohnson
Copy link
Author

chaddjohnson commented Mar 18, 2020

Actually, it appears that the following works:

showNotification: useField(useChoiceField(product.showNotificationBanner)),

If this is the correct way to utilize useChoiceField, it might make sense to update the documentation to reflect this.

@michaelmelanson
Copy link
Contributor

michaelmelanson commented Mar 18, 2020

Thanks for the bug report! I think it was my change in #1296 that caused this, so I'll look into it for you. Sorry for the trouble.

In the meantime I suggest going back to version 0.3.34 of react-form until this is resolved.

@chaddjohnson
Copy link
Author

chaddjohnson commented Mar 18, 2020

Thanks @michaelmelanson!

Also, I might have jumped the gun. When using

showNotification: useField(useChoiceField(product.showNotificationBanner)),

I am seeing the following for checkbox item values (instead of true/false) in formValues in the onSubmit handler passed to useForm():

Screen Shot 2020-03-18 at 12 44 04 PM

@chaddjohnson
Copy link
Author

@michaelmelanson Any news on this? Having this fixed would make react-form more usable.

@michaelmelanson
Copy link
Contributor

Sorry for the delay here. I meant to get to this sooner, but with everything else going on I haven’t yet.

It’s still on my radar, so I’ll get to it as soon as I can.

@chaddjohnson
Copy link
Author

chaddjohnson commented Apr 9, 2020

I found a workable solution.

For Checkbox fields, I'm using useField and asChoiceField:

const showNotifications = useField(props.product.showNotifications);

// ...

<Checkbox label="Show notifications" {...asChoiceField(showNotifications)} />

This works great. However, useChoiceField does not work:

const showNotifications = useChoiceField(props.product.showNotifications);

// ...

<Checkbox label="Show notifications" {...showNotifications} />

This results in Uncaught TypeError: Cannot convert undefined or null to object.

For ChoiceList fields, I am using useField:

const notificationOptions = useField(props.product.notificationOptions);

// ...

<ChoiceList
  choices={[
    {
      label: 'Always',
      value: 'ALWAYS'
    },
    {
      label: 'Never',
      value: 'NEVER'
    }
  ]}
  selected={notificationOptions.value}
  onChange={([value]) => notificationOptions.onChange(value)}
/>

@stale
Copy link

stale bot commented Oct 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Stale issue that hasn't received any attention in a while label Oct 4, 2020
@stale stale bot closed this as completed Oct 12, 2020
@avocadoslab
Copy link

@chaddjohnson that exactly solved my issue! Thanks a ton.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Package: react-form stale Stale issue that hasn't received any attention in a while Type: Bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants