I have given a list on teams I want to display using antd Input fields. An additional Input-field is created to be able to add a new team.
When I press 'enter' a given team should be updated or a new team should be added and a new, empty Input field should be created.
My code looks like the following
我已经给出了一个我想要使用antd输入域显示的团队列表。将创建一个额外的输入字段,以便能够添加新团队。当我按下‘Enter’时,应该更新一个给定的团队,或者应该添加一个新的团队,并且应该创建一个新的空输入字段。我的代码如下所示
`import { useState } from 'react'
import { Input } from 'antd'
const TeamListCreator = () => {
const [teamslist, setTeamslist] = useState(['Team1'])
const [newTeam, setNewTeam] = useState('')
const teamChange = (index) => {
const updatedTeams = [...teamslist]
updatedTeams[index] = newTeam
setTeamslist(updatedTeams)
}
return (
<div>
<h2>Teams hinzufügen</h2>
<p>Aktuell vohanden Teams: {teamslist.length}</p>
{teamslist.map((team, index) => (
<Input
key={index}
defaultValue={team}
onChange={(e) => setNewTeam(e.target.value)}
onPressEnter={(index) => teamChange(index)}
/>
))}
<Input
key={() => 'newTeam' + teamslist.length}
placeholder="Neues Team"
onChange={(e) => setNewTeam(e.target.value)}
onPressEnter={() => {
if (newTeam.trim() !== '') {
setTeamslist([...teamslist, newTeam])
setNewTeam('')
}
}}
/>
</div>
)
}
export default TeamListCreator`
It seems to work fine. But when I create a new team pressing 'enter' in the newTeam-Input field, the newly created (empty) Input field contains the value of the former "newTeam" Input field.
它似乎运行得很好。但是,当我在newTeam输入字段中按Enter键创建一个新团队时,新创建的(空)输入字段包含以前的“newTeam”输入字段的值。
My expectation was, that the value is empty and the placeholder is used.
我的预期是,该值为空,并且使用了占位符。
For me it also seems to be a rendering issue, as the value of field is empty.
对我来说,这似乎也是一个渲染问题,因为字段的值是空的。
Has anyone an idea, how to fix this?
有没有人有主意,怎么解决这个问题?
My expectation was, that the value is empty and the placeholder is used.
我的预期是,该值为空,并且使用了占位符。
For me it also seems to be a rendering issue, as the value of field is empty.
对我来说,这似乎也是一个渲染问题,因为字段的值是空的。
更多回答
优秀答案推荐
you have to use:
value={newTeam}
您必须使用:Value={newTeam}
for second input
对于第二个输入
更多回答
我是一名优秀的程序员,十分优秀!