Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

javascript - React input defaultValue re-rendering

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?

question from:https://stackoverflow.com/questions/65886119/react-input-defaultvalue-re-rendering

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...