- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Redux/Redux-Toolkit 的新手,我一直在尝试使用 RTK 为异步方法提供的 createAsyncThunk 方法获取一些数据,但我遇到了一些问题,我的有效负载刚刚“完成”返回空值。
我还能够 console.log 仅出于测试目的获取的数据,因此我知道我能够获取它。
我不确定这是我遇到的 javascript 问题还是 Redux 问题,所以非常感谢您的帮助!
如果有人有兴趣提供帮助,这里是我手上的一些代码片段!
// searchSlice.js
const fetchRequest = () => {
return fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
Authorization: 'Basic ' + btoa(client_id + ':' + client_secret),
},
body: 'grant_type=client_credentials',
})
}
// searchSlice.js
export const getArtistByQuery = createAsyncThunk('search/getArtistByQuery', async (query) => {
await fetchRequest()
.then((result) => result.json())
.then((data) => {
const token = data?.access_token;
const spotify = new SpotifyWebApi();
spotify.setAccessToken(token);
spotify.searchArtists(query, {limit: 10})
.then((data) => {
return data?.artists?.items
})
})
})
// Where I dispatch
// SearchInput.js
const handleSubmit = (e) => {
e.preventDefault();
dispatch(getArtistByQuery(query));
console.log(artists);
setQuery("");
}
return (
<Flex textAlign="center" m={5}>
<form onSubmit={handleSubmit}>
<FormControl>
<Input mb={5} type="text" onChange={handleChange} placeholder='Enter artist' size="md"/>
<Button type="submit">Click Me</Button>
</FormControl>
</form>
</Flex>
)
}
// createSlice
const artistsSlice = createSlice({
name: 'artists',
initialState,
extraReducers: builder => {
builder.addCase(getArtistByQuery.pending, state => {
state.loading = true
})
builder.addCase(getArtistByQuery.fulfilled, (state, action) => {
state.loading = false
console.log("Payload: " + action.payload)
state.artists = action.payload
state.error = ''
})
builder.addCase(getArtistByQuery.rejected, (state, action) => {
state.loading = false
state.artists = {}
state.error = action.error.message
})
}
})
// initialState
const initialState = {
loading: false,
artists: {},
error: ''
}
// store.js
export const store = configureStore({
reducer: {
artistsSlice: artistReducer,
},
});
最佳答案
而不是这个
export const getArtistByQuery =
createAsyncThunk('search/getArtistByQuery', async (query) => {
await fetchRequest().then
应该是这样
export const getArtistByQuery =
createAsyncThunk('search/getArtistByQuery', async (query) => {
return await fetchRequest().then()
关于javascript - 当我使用 createAsyncThunk 时,action.payload 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73793424/
有如下组件代码: import React from 'react'; import { useSelector, useDispatch } from 'react-redux'; import {
我有一个 thunk Action 是由 createAsyncThunk 创建的.我想在调用 api 更新状态之前调度一个 Action 。 我不想使用 Action getProducts.pen
所以我是 redux-toolkit 的新手,我想做一些非常简单的事情。我想通过这个辅助函数发送一些关于 POST 请求的数据。所以我尝试了这个 export const submitPaymentT
我正在使用 createAsyncThunk向某些 API 发出异步请求。在任何给定时刻,只有一个请求处于事件状态。 我知道可以使用提供的 AbortSignal 中止请求如果我从上一次调用中返回了
我是 redux 工具包库的新手,尤其是在测试方面。我浏览了文档并阅读了一堆关于这个主题的帖子和文章,但仍然很挣扎。我构建了一个简单的待办事项应用程序,并包含了几个 API 请求来涵盖异步情况。不过,
使用 Redux-Toolkit,我尝试在 createAsyncThunk 中使用 ThunkAPI 和分派(dispatch),但由于类型错误而被拒绝。不确定如何解决这个问题。 我的商店: exp
当 fetchUserById() 创建时,它的输出响应被定义。 const fetchUserById = createAsyncThunk( 'users/fetchByIdStatus',
我熟悉 react hooks,我发现使用 useEffect 真的很容易,thunk 很难处理,我可以只使用 useEffect 和 axios 并将结果发送到商店而不使用 createAsyncT
我正在从 Redux 迁移到 Redux Toolkit。我这里的简化代码用于使用 lodash/debounce 进行去抖动更新。 import debounce from "lodash/debo
我是 Redux/Redux-Toolkit 的新手,我一直在尝试使用 RTK 为异步方法提供的 createAsyncThunk 方法获取一些数据,但我遇到了一些问题,我的有效负载刚刚“完成”返回空
我有一个 react/typescript 项目。我正在使用 redux 来管理状态。 //store.ts import { configureStore, combineReducers } fr
有人可以像我 9 岁那样向我解释什么以及如何使用 createAsyncThunk 吗? Action 字符串有什么用?它是出于逻辑/路径原因临时创建并很快销毁吗?我可以用操作字符串做什么/人们通常用
我无法设置 getState() 的返回类型至RootState .我正在使用 typescript 和 VSCode。我必须将类型设置为 any ,这会停止该对象上的 IntelliSense。下面
环境 Redux 工具包 react typescript Grpahql 背景 我正在使用 redux 和 redux-toolkit 进行本地状态管理。我阅读了 redux-toolkit 文档。
环境 Redux 工具包 react typescript Grpahql 背景 我正在使用 redux 和 redux-toolkit 进行本地状态管理。我阅读了 redux-toolkit 文档。
我想为我工作的项目中的用户创建一个 Redux 切片。我有 this code sandbox而且我不知道为什么 MyButton.tsx 文件中的 fetchAll 调用会出现以下错误: fetch
我在看 createAsyncThunk文档,感觉有点与流程混淆。这是来自文档: import { createAsyncThunk, createSlice } from '@reduxjs/too
我对 redux 工具包还很陌生,所以我仍然有一些问题! 根据下面的代码,我正在尝试访问我的 loginDetails.username 中的状态( loginDetails.password 和 c
我目前正在设置我的 RTK (Redux Toolkit) 并进行了一些小测试。这是我的代码: 存储/index.js import { configureStore } from '@reduxjs
我刚开始使用 Redux Toolkit(RTK),我很难找到从 RTK 使用 createAsyncThunk 的正确方法。 所以我想做的是 调度由 createAsyncThunk 创建的异步操作
我是一名优秀的程序员,十分优秀!