jaredpalmer / jaredpalmer/formik

An undefined prop does not equal a non existing prop

Open
#3,293 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
34.3k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

Bug report

Current Behavior

When a Field's value and onChange props are set to undefined the default behavior is still overwritten.
This can be relevant for custom input fields.
Example:

function CustomTextInput(props) {
    return (
        <div>
            <Field
                name={props.name}
                value={props.value}
                onChange={props.onChange}
            />
        </div>);
}

function CustomTextInput() {
    return (
        <>
            <Field
                name={"firstname"}
                value={"Johnny"}
                onChange={console.log}
            />
            <Field
                name={"lastname"}
            />
        </>
    );
}

Now, for the second input lastname no initial value is set and the value cannot be changed (it will be displayed, but not actually be set). I guess a Field's props are not checked for undefined, but rather for existence.
Probably something like "value" in props instead of props.value !== undefined.

I would see this as a bug, that forces the use of workarounds. In my personal opinion undefined should be equal to a non existing value.

Full example: https://codesandbox.io/s/awesome-water-ccijl?file=/index.js

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 linked CodeSandbox and reproduce the behavior using the two Field examples, focusing on how value and onChange passed as undefined affect Field. Trace the Field prop handling and verify that omitting those props and passing them as undefined produce the intended default behavior without preventing lastname from being changed.

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.