reactjs - TypeError:AddTag 不是函数
问题描述
我正在尝试将标签用户输入添加到现有标签列表中,但我不断收到 TypeError。这是代码的简单版本。我在 App.js 文件中创建了 addTag 并在 Profil.js 中调用它
在 App.js 中
import tag from './tag.json'
const [tagList, setTagList] = useState(tag);
const addTag = (tagInput) => {
console.log("adding..")
const newTag = { id : Date.now(), tag: tagInput}
setTagList([...tagList,newTag])
console.log("added..")
}
return (
<Profile key={e.id} name={e.name} username={e.username} email={e.email} website={e.website} company={e.company.name}
street={e.address.street} suite={e.address.suite} city={e.address.city} zipcode={e.address.zipcode}
addTag={addTag} /> )
在 Profile.js 中
const Profile = (user,{addTag}) => {
const [tagInput, setTagInput] = useState('')
const handleChange = (e) => {
setTagInput(e.currentTarget.value)
}
const handleSubmit = (e) => {
e.preventDefault()
addTag(tagInput)
setTagInput("")
}
return (
<div className="search">
<input type="text" placeholder="Add a tag" value={tagInput} onClick={()=>{
setCancelTag(!cancelTag)
console.log("search enabled")
}} onChange={handleChange} />
{cancelTag? null : <button className="cancel-tag" onClick={handleSubmit} >×</button>}
</div>
}
解决方案
试试下面的。这将解构addTag
prop 然后将其余属性分配/传播user
给 name 变量:
const Profile = ({ addTag, ...user }) => {
请记住,所有道具都作为第一个参数Profile
作为对象传递给函数,其属性与传递给的道具名称匹配Profile
。
推荐阅读
- microsoft-graph-api - Microsoft Graph - 如何获取共享点站点
- delphi - delphi 如何使用笔压属性?
- java - 子域下的重定向问题
- dictionary - 在函数中从 Hashmap 正确返回 Int
- c# - 如何为每个请求更改我的架构?
- swift - 带有远程 URL 的 Swift CSVImporter 框架
- c# - 如何将 s-maxage 添加到 Web、配置缓存配置文件
- forms - 使用电子表格中最后记录的响应中的数据预填充谷歌表单字段
- java - 如何使用 Wildfly 上的远程 EJB 将有关调用者/客户端的一些信息发送到服务器端?
- wordpress - Wordpress - WP Query:meta_query 关系被忽略