【问题标题】:Adding Multiple Divs and Inputting Values with React/Material UI使用 React/Material UI 添加多个 div 和输入值
【发布时间】:2021-09-27 12:53:24
【问题描述】:

我在将多个 div 添加到 React 页面并将输入的值发送到 API 时遇到问题。现在我有一个按钮,它添加了一个 div,其中有一个文本输入框,用户可以输入答案。但是,每次制作一个新盒子时,用户的答案都会根据状态存储,每次接收一个字母。下面是生成的新 div 的代码:

const AnotherName = (key) => (
<div id={countOfOtherNames}>
  <GridContainer>
    <GridItem xs={6} sm={6} md={6}>
      <h4>What other animal are you looking for?</h4>
      <div className={classes.inputbar}>
        <InputBase
          placeholder="Enter name"
          classes={{
            root: classes.inputRoot,
            input: classes.inputInput,
          }}
          inputProps={{ 'aria-label': 'description' }}
        />
      </div>
      <br />
    </GridItem>
  </GridContainer>
</div>

);

我不知道如何在每次打开盒子时创建多个状态,但这似乎是一种幼稚的做法。有没有更好的方法在用户完成输入后存储每个输入?

【问题讨论】:

    标签: reactjs input material-ui


    【解决方案1】:

    您可以将输入值存储在对象中,并在焦点更改时保存。

    【讨论】:

    • 你能详细说明一下吗?在这种情况下,焦点变化是什么?
    • 输入元素有一个 onFocus 属性来处理事件:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2021-02-16
    • 2017-08-03
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多