【发布时间】:2021-03-31 09:15:55
【问题描述】:
我目前正在尝试使用 Spotify API 访问我的数据。这很好用。这就是我用来获取数据的功能。我认为其他的东西并不重要。如果你需要,我可以发布。
export const getSpotifyUser = (access_token:string) =>{
setAuthorizationHeader(access_token)
axios.get('https://api.spotify.com/v1/me').then((res) => {
console.log(res.data)
})
}
我已经建立了一个 redux 存储并尝试通过调度正确的类型 (SET_USER) 将凭据放入存储中。
export const getSpotifyUser = (access_token:string) => (dispatch: any) => {
console.log("function is not called") // Function is not even called why ?
setAuthorizationHeader(access_token)
axios.get('https://api.spotify.com/v1/me').then((res) => {
console.log(res.data)
dispatch ({
type: SET_USER,
payload: res.data
})
}
但是一旦我使用了 dispatch,这个函数就不再被调用了。 我真的没有看到我的错误。那是打字稿错误吗? (我正在使用反应打字稿)
store.js
import { createStore, applyMiddleware } from 'redux'
import rootReducer from './rootReducer'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'
const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(thunk))
)
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch
export default store
rootReducer.ts
import { combineReducers } from 'redux'
import userReducer from './User/userReducer'
const rootReducer = combineReducers({
user: userReducer,
})
export default rootReducer
userReducer.ts
import { AnyAction } from 'redux'
import { SET_USER } from './userTypes'
interface Credentials {
username: string
email: string
profilepicture: string
id: number
}
interface InitialState {
authenticated: boolean
loadding: boolean
credentials?: Credentials
}
const initialState: InitialState = {
authenticated: false,
loadding: false,
credentials: {} as Credentials,
}
const reducer = (state = initialState, action: AnyAction) => {
switch (action.type) {
case SET_USER: {
return {
...state,
loading: false,
credentials: action.payload,
}
}
default:
return state
}
}
export default reducer
Login.tsx(我在这里登录。它工作正常,如果不使用调度
import { IonButton } from '@ionic/react'
import React, { useEffect } from 'react'
import {
getAuthorizeHref,
getHashParams,
removeHashParamsFromUrl,
getSpotifyUser,
} from '../../Helpers/login'
const Login: React.FC = () => {
// const user = useSelector((state: RootState) => state.user.credentials)
useEffect(() => {
const hashParams = getHashParams()
const access_token = hashParams.access_token
// const expires_in = hashParams.expires_in
removeHashParamsFromUrl()
getSpotifyUser(access_token)
}, [])
return (
<IonButton onClick={() => window.open(getAuthorizeHref(), '_self')}>
)}
export default Login
【问题讨论】:
-
你的编译器告诉你什么?我不知道 redux,但我很确定你的 dispatch 参数不应该放在这里,那是错误的语法。据我了解,您应该在对象上调用 dispatch 方法。
-
你确定吗?这就是我学到的方式。我没有使用 dispatch Hook 来明确。 youtu.be/m_u6P5k0vP0?t=22935 。例如,他也在这样做
-
能否请您发布您的意思的代码? :)
-
好吧,实际上语法是错误的,你忘了第二个 => 箭头函数 man。但是你应该有一个编译器来告诉你,或者至少记录你的打字稿错误。
-
哦,对不起,我刚刚删除了第二个 =>,而我正在写我的帖子
标签: javascript reactjs typescript react-redux