【发布时间】:2020-10-20 22:48:13
【问题描述】:
我正在尝试使用 redux 来更改我的反应状态。我的其他行动案例似乎运作良好,但我坚持这一点。我认为问题的根源是异步功能,但我不确定。我根本无法使用异步函数中的 return 语句更改状态。我最好的猜测是,return 语句只能在与“if”语句相同的范围内工作,因此我不能从异步函数内部返回来更改状态。如果这就是我如何从异步函数中获取值并将其在状态中上移一级以返回它的原因?如果问题与范围无关,那么我也全神贯注。
itemReducer.js:
case ADD_ITEM:
if (action.payload.match(/soundcloud.com/) == "soundcloud.com") {
// First async await
async function newItem() {
console.log("0")
const response = await fetch("https://soundcloud.com/oembed?format=json&url=" + action.payload );
const json = await response.json();
let html = json.html.match(/(?<=url=).{53}/).toString()
let newItem = {
id: uuidv4(),
url: html,
name: json.title,
isOpen: false
}
console.log(newItem, ...state.items)
return {
items: [...state.items, newItem]
}
}
newItem();
}
【问题讨论】:
标签: javascript reactjs react-native redux react-redux