0

I'm using form with react and I'm having trouble updating my input values. I have two listing buttons. These buttons allow me to call different json objects and transfer them to the input. And I use these buttons respectively. Some json objects may have similar keys. There is no change in the inputs where these similarities are transferred. While there is no problem in different keys, there is no change in the input for the same keys. I write values from json as defaultValue to the input. There is no change in defaultValues. I used a placeholder to test it. the placeholder continues to work without any problems. However, defaultValue is not updated.

{Object.keys(props.activeElement).map((key) => (
          <Form.Item label={key} name={key}>
            {inputRender(key)}
          </Form.Item>
  ))}

When the relevant button is clicked, the returned jsons are set to the activeElement.

const inputRender = (key) => {
    return <Input defaultValue={props.activeElement[key]} />;
  };

Can you help me?

8
  • It is hard to say with the little code you shared but, from your description, I'd expect activeElement[key] to come from state (which will probably be updated every time you click on a button) rather than from props (which is static). Commented Jan 25, 2021 at 13:57
  • Can you provide a reproducible example? Commented Jan 25, 2021 at 13:58
  • Unfortunately I get the same result with state @secan Commented Jan 25, 2021 at 14:04
  • Add a key to the element and change that each time you need the value to update, it will force the element to rerender. Commented Jan 25, 2021 at 14:30
  • So how can I do that. Can you help me @Michael Commented Jan 25, 2021 at 14:57

3 Answers 3

0

The defaultValue is cached on the first render. So even when props.activeElement[key] points to a new value, the input's value doesn't change.

More about that on antd's FAQ.

Sign up to request clarification or add additional context in comments.

5 Comments

So do you have a preference suggestion for this?
Why not use value and onChange as you have controlled components.
I am not using onChange because I am using formOnChange. When I use Value, the inputs return empty.
@Gucal Did you try using initialValues on the Form?
I tried but unfortunately this did not solve my problem :(
0

I solved the problem with the use of ref. I created a ref value.

const formRef = useRef ();

On button click i ran this.

formRef.current.setFieldsValue ({... json});

And I used this refi on my form.

ref = {props.formRef}

Comments

0

For components like input etc. which maintain their own states there are attributes that don't change for the life-time of the component and therefore the default value will become permanent state of the component (Refer: Legacy but still applicable document).

When React renders a component by position, it always takes the state of the component stored in React. Therefore as long as the same component (in this case input) is rendered in the same position, it will render with the same React state. (Refer: Preserving & Resetting State).

One way to make the default value to be rendered is to ensure that it is not the same component, for which a 'key' property needs to be added to the input.

In the below code a key is set to a new UUID (uuid - npm), which essentially forces React to reset the entire component state in the memory and render it as a new component.

<input type="text" 
       name="proc_name" 
       placeholder="This is the name" 
       defaultValue={procName} 
       key={uuidv4()}/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.