Javascript 迭代数组&;Reactjs中方法调用内的null检查
我想替换道具,我想从detailsItem数组中获取id、name和designationJavascript 迭代数组&;Reactjs中方法调用内的null检查,javascript,arrays,reactjs,typescript,Javascript,Arrays,Reactjs,Typescript,我想替换道具,我想从detailsItem数组中获取id、name和designation sampleItem = () => { this.sampleService .sampleMethod(this.props.id, this.props.name, this.props.designation) .then((response) => {}) .catch((error) => { console.log(error);
sampleItem = () => {
this.sampleService
.sampleMethod(this.props.id, this.props.name, this.props.designation)
.then((response) => {})
.catch((error) => {
console.log(error);
});
};
我尝试移除道具并在sampleItemOne中调用id、名称和名称,但出现了一个错误:预期会有一个赋值或函数调用,而看到了一个表达式有人能帮我解决这个问题吗,我做错了什么 在下面的一段代码中,您在三元组周围添加了一个额外的
{}
。这可能是因为您习惯于在JSX中使用它来表示表达式,但在这样的函数体中,它的对象表示法
sampleItemOne = () => {
this.state.detailsItem.map((item) => {
{ item? (
this.sampleService
.sampleMethod(item.id, item.name, item.designation)
.then((response) => {})
.catch((error) => {
console.log(error);
})
: null;
}
});
};
因为上面的代码不是有效的对象,所以这是中断的
如果,只需使用常规的(无论如何,它的可读性更高)
this.state.detailsItem.map((项)=>{
如果(项目){
这是我的样品服务
.取样方法(项目id、项目名称、项目名称)
.然后((响应)=>{})
.catch((错误)=>{
console.log(错误);
});
}
});
我删除了它,但如果我在catch块的closing语句之后加上“;”我会得到“parsing error”,”,仍然会得到相同的错误,但我认为不应该在方法内部迭代这些值。这将调用与数组中的项数相等的方法!
sampleItemOne = () => {
this.state.detailsItem.map((item) => {
{ item? (
this.sampleService
.sampleMethod(item.id, item.name, item.designation)
.then((response) => {})
.catch((error) => {
console.log(error);
})
: null;
}
});
};
this.state.detailsItem.map((item) => {
{item ? (
this.sampleService
.sampleMethod(item.id, item.name, item.designation)
.then((response) => {})
.catch((error) => {
console.log(error);
})
: null;
}
});