反应附加孩子
我试图理解为什么我的代码不断失败,我有3个输入,我试图在带有React的待办事项列表中显示为项目。错误说“附录不是函数”。
我已经看到了在普通旧JS上使用的相同代码,并且我正在尝试将其用于React。
我真的是JS和反应的新手,所以请尽可能地解释任何可能的答复。
const cargarDatos = (pool) => {
pool.preventDefault()
const valueList = datos.text + ' ' + datos.amount + ' ' + datos.date
const list = List.document.getElementsByClassName('.div-list')
const itemList = document.createElement("div")
console.log(valueList)
itemList.innerText = valueList
itemList.classList.add('item-List')
list.appendChild(itemList)
}
列出JSX组件
import React from "react";
export function List () {
return (
<>
<h2>List</h2>
<div className={'div-list'}></div>
</>
)
}
对不起我的英语。
I'm trying to understand why my code keeps failing, I have 3 inputs which I'm trying to make appear as the item in a TODO list with React. The error say "appendChild is not a function".
I've seen this same code working on plain old JS, and I'm trying to use it on React.
I'm really new to JS and React, so please explain any possible reply as basic as you can.
const cargarDatos = (pool) => {
pool.preventDefault()
const valueList = datos.text + ' ' + datos.amount + ' ' + datos.date
const list = List.document.getElementsByClassName('.div-list')
const itemList = document.createElement("div")
console.log(valueList)
itemList.innerText = valueList
itemList.classList.add('item-List')
list.appendChild(itemList)
}
List JSX component
import React from "react";
export function List () {
return (
<>
<h2>List</h2>
<div className={'div-list'}></div>
</>
)
}
Sorry for my English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
document.getElementsbyClassName
函数始终返回一个数组,这就是为什么您的代码无法正常工作的原因。如果您知道类
div-list
只有一个元素,则可以通过选择数组中的第一个元素来修复它。The
document.getElementsByClassName
function always returns an array that is why your code isn't working.If you know there is only one element with the class
div-list
you can fix it by selecting the first element in the array.