仅 onChange 和映射数组中当前输入的值

我只是有一个映射输入的数组。我想使用 useState 仅更改当前输入的值。以正常方式执行此操作会更改映射数组中每个输入的输入,这是我不想要的。


代码示例 -


const [text, setText] = useState("");


comments.map((c) => (

  <Row key={c._id}>

    <Form

      onSubmit={(e) => {

        e.preventDefault();

        addComment(comment._id, { text });

        setText("");

      }}

    >

      <InputGroup className="mb-3">

        <FormControl

          placeholder="add a comment"

          aria-label="add a comment"

          aria-describedby="inputGroup-sizing-default"

          value={text}

          onChange={(e) => setText(e.target.value)}

        />

      </InputGroup>

    </Form>

  </Row>

));

目前的方式我将更改所有映射的输入的每个文本值,这是错误的。


对于简单的问题我找不到答案,对此表示歉意。


喵喵时光机
浏览 172回答 3
3回答

人到中年有点甜

您需要使用数组而不是字符串作为文本状态,因为每个评论都有状态const [text, setText] = useState(new Array(comments.length));comments.map((c,i) => (&nbsp; <Row key={c._id}>&nbsp; &nbsp; <Form&nbsp; &nbsp; &nbsp; onSubmit={(e) => {&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; addComment(comment._id, { text });&nbsp; &nbsp; &nbsp; &nbsp; setText(new Array(comments.length));&nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; <InputGroup className="mb-3">&nbsp; &nbsp; &nbsp; &nbsp; <FormControl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="add a comment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aria-label="add a comment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aria-describedby="inputGroup-sizing-default"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={text[i]}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={(e) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const newText = [...text];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newText[i] = e.target.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(newText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; </InputGroup>&nbsp; &nbsp; </Form>&nbsp; </Row>));由于状态是数组,现在您需要将值设置为text[i],当您更改状态时,您需要更改元素上的i文本

红颜莎娜

您需要更改状态以在某处包含文本字符串数组,而不仅仅是单个字符串。然后使用正在迭代的元素的索引来确定要设置什么值以及要在更改时更改什么索引。我认为最优雅的解决方案是更改comments对象以包含text属性,例如:{&nbsp; _id: 'somekey',&nbsp; text: ''&nbsp; // ...}然后映射它们:const setCommentText = (text, i) => setComments(&nbsp; comments.map(&nbsp; &nbsp; (comment, i) => comment !== i ? comment : { ...comment, text }&nbsp; ));comments.map((c, i) => (&nbsp; <Row key={c._id}>&nbsp; &nbsp; <Form onSubmit={e => {&nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; addComment(comment._id, {c.text});&nbsp; &nbsp; &nbsp; setCommentText('', i);&nbsp; &nbsp; }}>&nbsp; &nbsp; &nbsp; <InputGroup className="mb-3" >&nbsp; &nbsp; &nbsp; &nbsp; <FormControl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="add a comment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aria-label="add a comment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aria-describedby="inputGroup-sizing-default"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={c.text}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={e => setCommentText(e.target.value, i)}&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; </InputGroup>&nbsp; &nbsp; </Form>&nbsp; </Row>));

墨色风雨

试试这个方法const [inputValues, setInputValues] = useState([]);const updateValue = (value, index) => {&nbsp; const newInputValues = [... inputValues];&nbsp; newInputValues[index] = value&nbsp; setInputValues(newInputValues);}comments.map((c, index) => (&nbsp; <Row key={c._id}>&nbsp; &nbsp; ....&nbsp; &nbsp; &nbsp; <InputGroup className="mb-3">&nbsp; &nbsp; &nbsp; &nbsp; <FormControl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .......&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={inputValues[index] || ""}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={(e) => setInputValues(e.target.value)}&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; </InputGroup>&nbsp; &nbsp; </Form>&nbsp; </Row>));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript