如何在React元素的div类中定义键?

huangapple go评论71阅读模式
英文:

how to define keys within div class of react elements?

问题

我正在学习React-Js并创建一个表单。在调试过程中,我发现问题可能是没有为表单中的每个字段定义唯一的键。当我在表单中添加新记录时,无法看到字段中的值发生变化。以下是我的字段代码:

const AddTask = ({ onAdd, selectedTask, isReadOnly }) => {
  console.log("selectedTask", selectedTask);
  const [dte, setdte] = useState("");
  const [rmk, setrmk] = useState("");
  const [isChecked, setIsChecked] = useState(false);

  const onSubmit = (e) => {
    const data = {
      dte,
      rmk,
    };
    onAdd(data);
    setdte("");
    setrmk("");
  };

  return (
    <form className="add-form" onSubmit={onSubmit}>
      <div className="form-group row">
        <label className="col-sm-2 col-form-label">Date </label>
        <div className="col-sm-8">
          <input
            className="form-control"
            type="Date"
            placeholder="Add"
            value={selectedTask?.dte || ""}
            onChange={(e) => setdte(e.target.value)}
            disabled={isReadOnly}
          />
        </div>
      </div>
      <div className="form-group row">
        <label className="col-sm-2 col-form-label">Remarks </label>
        <div className="col-sm-8">
          <input
            className="form-control"
            type="text"
            value={selectedTask?.rmk || ""}
            onChange={(e) => setrmk(e.target.value)}
            disabled={isReadOnly}
          />
        </div>
      </div>
    </form>
  );
};

希望这有助于你解决问题。

英文:

I am working on a form in react-Js as a beginner. During debugging I came to know the problem could be not defining the unique keys for every field in the form. I am not able to see the values changing in the field when adding new records in the form. here is my code for fields

const AddTask = ({ onAdd, selectedTask,isReadOnly }) =&gt; {
console.log(&quot;selectedTask&quot;,selectedTask);
const [dte, setdte] = useState(&quot;&quot;);
const [rmk, setrmk] = useState(&quot;&quot;);
const [isChecked, setIsChecked] = useState(false);
const onSubmit = (e) =&gt; {
const data = {
dte,
rmk,
}
onAdd(data); 
setdte(&quot;&quot;);  
setrmk(&quot;&quot;);
};
return (
&lt;form className=&quot;add-form&quot; onSubmit={onSubmit}&gt;
&lt;div className=&quot;form-group row&quot;&gt;
&lt;label className=&quot;col-sm-2 col-form-label&quot;&gt;Date &lt;/label&gt;
&lt;div className=&quot;col-sm-8&quot;&gt;
&lt;input
className=&quot;form-control&quot;
type=&quot;Date&quot;
placeholder=&quot;Add&quot;
value={selectedTask?.dte || &quot;&quot;}
onChange={(e) =&gt; setdte(e.target.value)}
//ref={dteRef}
disabled={isReadOnly}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;form-group row&quot;&gt;
&lt;label className=&quot;col-sm-2 col-form-label&quot;&gt;Remarks &lt;/label&gt;
&lt;div className=&quot;col-sm-8&quot;&gt;
&lt;input
className=&quot;form-control&quot;
type=&quot;text&quot;
value={selectedTask?.rmk || &quot;&quot;}
// ref={rmkRef}
onChange={(e) =&gt; setrmk(e.target.value)}
disabled={isReadOnly}
/&gt;
&lt;/div&gt;
&lt;/div&gt;

答案1

得分: 0

以下是翻译好的部分:

它与定义键没有任何关系,我们只需要更改值中条件的优先级如下:

value={ dte ||selectedTask?.dte }
value={ rmk || selectedTask?.rmk}
对于所有输入字段,这将解决问题。

英文:

it have nothing to do with defining keys we just have to change the priority of condition in value as:

value={ dte ||selectedTask?.dte }
value={ rmk || selectedTask?.rmk} 

for all input fields it will solve the problem

huangapple
  • 本文由 发表于 2023年5月15日 14:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定