marmelab / marmelab/react-admin

UseWatch with an input field that has a default value returns undefined when the input field is not changed

Open
#9,419 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
TypeScript
Stars
26.9k
Forks
5.5k
Avg merge
2d 3h
Merged PRs (30d)
19

Description

**What you were expecting:**
I would expect that useWatch returns the default value of the field.

**What happened instead:**
useWatch returns undefined, untill I change the watched field.

**Steps to reproduce:**

create a form with an input that has a default value, then have another component use useWatch on that input field, the resulting value is undefined untill you change the input.

**Related code:**
I created a WatchBug component in the PostCreate file. It watches the Published at input. you see that initally the value returned is undefined.
https://stackblitz.com/edit/github-mjvobu?file=src%2Fposts%2FPostCreate.tsx

**Environment**

* React-admin version: 4.15.2
* Last version that did not exhibit the issue (if applicable): 4.8.4 does not show the bug, I did not try other versions inbetween.

Working version on 4.8.4
https://stackblitz.com/edit/github-ladv9x?file=src%2Fposts%2FPostCreate.tsx

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the WatchBug component in src/posts/PostCreate.tsx from the linked StackBlitz and inspect how useWatch observes the Published at input. Reproduce the behavior on react-admin 4.15.2 and compare it with 4.8.4; done means useWatch returns the field's default value before the input changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.