I'm updating status if there is a condiotion of comment?.length, I wanna only update comment if there is a length of comment, everything works fine, but I have a question. Can I do this condition easier? I mean without mapping 2 times.
const changeStatus = (id, status,comment) => {
axios
.post("http://localhost:5000/changeStatus", {
id: id,
status: status,
comment: comment,
})
.then((res) => {
comment?.length
? setStatus(
status.map((val) => {
return val.id == id
? {
...val,
status: status,
comment: comment
}
: val;
})
) : setStatus(
status.map((val) => {
return val.id == id
? {
...val,
status: status,
}
: val;
})
);
CodePudding user response:
I think you can just map once and put the condition inside the object
.then((res) => {
setStatus(status.map((val) => {
if(val.id === id){
return {
...val,
status: status,
comment: comment.length ? comment : null
}
}
return val
})
}