我正在尝试将变量“结果”放入操作有效负载中,但无法定义它,因为它位于 try 块中。我不确定如何解决这个问题。我还是 Redux 的新手,这就是操作本身。
export const fetchMovies = async (endpoint, category) => {
const isLoadMore = endpoint.search('page');
try{
const result = await (await fetch(endpoint)).json();
} catch(error){
this.setState({error: true});
console.log(error);
}
return{
type: HomeActionTypes.FETCH_MOVIES,
payload: result, category, isLoadMore
}
}
我尝试使用 let 在顶部初始化结果,但它没有解决问题。另外,我不确定我是否使用我在其中设置的变量正确设置了有效负载。例如,它们需要在 reducer 中使用,我的猜测是将有效负载中的项目称为 action.payload.result、action.payload.category、action.payload.isLoadMore,只要我需要在 reducer 中使用它们. 这是正确的方法吗?感谢您为帮助回答我的问题所做的任何贡献。