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.

initialValue of useField() hook resets input.value of the field

See original GitHub issue

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

const Component = () => {
  useField("firstName", { subscription: { value: true }, initialValue: "Max" });
  return (
    <div>
      <label>First Name (new)</label>
      <Field
        name="firstName"
        component="input"
        type="text"
        placeholder="First Name"
      />
    </div>
  );
};

When changing the input value of firstName Field to e.g. ‘Max abc’ and afterwards replacing this Component with an identical ComponentCopy (same fieldName and both using the useField() hook) the changed input.value gets reset to the initialValue: "Max" and therefore looses its changes.

Kapture 2019-06-14 at 16 20 41

What is the expected behavior?

Expected to work like <Form initialValues={{ firstName: "Max" }} ...> (which I added to the Sandbox demo at the bottom). When changing the input value of firstName Field to e.g. ‘Max abc’ and afterwards replacing this Component with an identical ComponentCopy (same fieldName, both use useField() hook to supply initialValue) the changed input.value (which is still part of the form) gets preserved and ComponentCopy displays the correct input.value ‘Max abc’ instead of the initialValue: "Max".

Kapture 2019-06-14 at 16 18 30

Sandbox Link

firstName demonstrates the bug and lastName presents a hacky solution to get the expected behavior. At the bottom a standalone form demonstrates the expected behaviour by supplying the initialValues to the <Form .. directly which is at the moment not achievable by using the useField() hook. https://codesandbox.io/s/react-final-form-simple-example-dmh3h?fontsize=14

What’s your environment?

"final-form": "4.14.1",
"react-final-form": "6.2.0",
"node": ">=10.16.0",

Other information

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ndrsllwngrcommented, Jun 17, 2019
// useField.js
import React from 'react';
import { useField as _useField, useForm } from 'react-final-form';

export const useField = (name, config) => {
  const form = useForm();
  const [alreadyRegistered] = React.useState(
    () => form.getFieldState(name) !== undefined,
  );

  if (alreadyRegistered) {
    // eslint-disable-next-line no-unused-vars
    let initialValue;
    // eslint-disable-next-line prefer-const, no-param-reassign
    ({ initialValue, ...config } = config);
  }

  return _useField(name, config);
};
// LabelEditor.js
import { useField } from './useField';
// ...
const { input } = useField(`${element.id}.label`, {
    type: 'text',
    subscription: { value: true },
    initialValue: get(element, 'state.label'),
  });

@Andarist thank you for your solution! I have tested it with our setup, and for now, it suffices as an intermediate workaround.

Nevertheless, I believe that fixing this issue at the useField() level as well as <Field .. level #536 would improve and simplify the UX of this remarkable form library.

2reactions
Andaristcommented, Jun 15, 2019

You have explained the problem really well! I feel like this could be supported - but I’m going to leave that decision up to @erikras .

By stretching a little bit current APIs I’ve managed to prepare a working solution for you though - https://codesandbox.io/s/react-final-form-simple-example-g9gqf - which can be used as an intermediate workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useField() - Formik
A custom React Hook that returns a 3-tuple (an array with three elements) containing FieldProps , FieldMetaProps and FieldHelperProps . It accepts either...
Read more >
How do I set an initial value for a text input when using a ...
You can do it by add defaultVal when declare enteredValue : const [ enteredValue, setEnteredValue ] = useState(defaultVal || "");.
Read more >
useField() hook - Formiz
This will update the isValidating state at the field level and also at the form and step levels. Notify the start of async...
Read more >
useField | Usetheform - An easy way for building forms in React
Specifies the initial value of an input element. checked : boolean. Specifies whether an input element should be pre-selected or not (for type="checkbox"...
Read more >
useFieldArray - Simple React forms validation
React hooks for Field Array ... Custom hook for working with Field Arrays (dynamic form). ... The input value will be registered during...
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