foreach 中的值推送未定义?
values inside foreach pushes undefined?
所以我正在处理一个 tsx 文件,我试图将一些 html 推入一个带有选项的变量中,该变量从 foreach 项目中获取它们的值,到目前为止一切顺利,但后来我尝试将它们推入进入某个数组并在我的状态下使用它,以便为它推送未定义的反应做正确的事情。我很困惑当我尝试将它推入数组时它如何给出 undefined 并且当我尝试将它推入 html element
时它给出值
provider.getItems().then((items:any[]) => {
items.forEach((item) => {
this.categoryOptItems.push(item.Title);
this.categoryOpts.push(<option value={item.Title}>{item.Title}</option>)
})
this.setState({categories:
this.categoryOptItems
})
})
我找到了我的解决方案,但不确定它是否是最好的方法。
这是在 componentDidMount
provider.getItems().then((items:any[]) => {
items.forEach((item) => {
categoryOptItems.push(item.Title);
})
this.setState({categories:
categoryOptItems
})
})
这是函数
options() {
var categories = this.state.categories;
const html:any = [];
for (var i = 0; i < this.state.categories.length; i++) {
html.push(<option value={i+1}>{categories[i]}</option>);
}
return html;
}
所以我正在处理一个 tsx 文件,我试图将一些 html 推入一个带有选项的变量中,该变量从 foreach 项目中获取它们的值,到目前为止一切顺利,但后来我尝试将它们推入进入某个数组并在我的状态下使用它,以便为它推送未定义的反应做正确的事情。我很困惑当我尝试将它推入数组时它如何给出 undefined 并且当我尝试将它推入 html element
时它给出值provider.getItems().then((items:any[]) => {
items.forEach((item) => {
this.categoryOptItems.push(item.Title);
this.categoryOpts.push(<option value={item.Title}>{item.Title}</option>)
})
this.setState({categories:
this.categoryOptItems
})
})
我找到了我的解决方案,但不确定它是否是最好的方法。
这是在 componentDidMount
provider.getItems().then((items:any[]) => {
items.forEach((item) => {
categoryOptItems.push(item.Title);
})
this.setState({categories:
categoryOptItems
})
})
这是函数
options() {
var categories = this.state.categories;
const html:any = [];
for (var i = 0; i < this.state.categories.length; i++) {
html.push(<option value={i+1}>{categories[i]}</option>);
}
return html;
}