Last active
January 25, 2023 19:03
-
-
Save seansean11/196c436988c1fdf4b22cde308c492fe5 to your computer and use it in GitHub Desktop.
Using Redux Thunk with Typescript (example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ActionCreator } from 'redux' | |
import { ThunkAction } from 'redux-thunk' | |
import { WebService } from 'app/services/WebService' | |
// Create this reusable type that can be imported into your redux action files | |
export type ThunkResult<R> = ThunkAction<R, RootState, Services, RootAction> | |
// Services are only necesarry because we are using | |
// `reduxThunk.withExtraArgument({ webService }))` when we are creating our store. | |
export type Services = { | |
webService: WebService | |
} | |
// Union type of all of your actions | |
export type RootAction = UserAction | |
// Interface of your root Redux state | |
export interface RootState { | |
readonly user: UserState | |
} | |
// Enum of your action types | |
export enum ActionType { | |
UserUpdate = 'USER_UPDATE', | |
} | |
export interface UserAction { | |
readonly type: ActionType | |
readonly user: Partial<UserState> | |
} | |
export interface UserState { | |
readonly groups: Array<string> | |
readonly isLoading: boolean | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Thunk, | |
ActionType, | |
UserAction, | |
UserState, | |
} from './storeTypes' | |
export const updateUser = (user: Partial<UserState>): UserAction => ({ | |
type: ActionType.UserUpdate, | |
user | |
}) | |
// ***** USING THUNKRESULT TYPE HERE ***** // | |
// this ThunkResult is `void` because it doesn't return anything | |
// if you return something from the thunk, replace `void` with the return value `ThunkResult<void>` | |
export const login = (email: string, password: string): ThunkResult<void> => async ( | |
dispatch, | |
getState, | |
{ webService } | |
) => { | |
try { | |
const { jwt, groups } = await webService.signIn( | |
email, | |
password | |
) | |
webService.setBearerToken(jwt) | |
dispatch(updateUser({ groups })) | |
} catch (err) { | |
throw err | |
} | |
} |
@n3rd253 that isn't a TS compile error, that is a JavaScript error. It's just trying to tell you that on line 9
of your FetchCategories.tsx
file, you are calling someObject.getCategories
, where someObject
(whatever it happens to be called in your codebase) is actually undefined
.
Im confused because it is defined and shows as a parameter of type Service
as youve declared your webservice in the example.
But it doesnt seem to resolve during runtime when the action is actually
fired.
I registered it with the store as youd shown
…On Wed, Apr 24, 2019, 7:58 AM Sean Michael ***@***.***> wrote:
@n3rd253 <https://github.com/n3rd253> that isn't a TS compile error, that
is a JavaScript error. It's just trying to tell you that on like 9 of
your FetchCategories.tsx file, you are calling someObject.getCategories,
where someObject (whatever it happens to be called in your codebase) is
actually undefined.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/196c436988c1fdf4b22cde308c492fe5#gistcomment-2897178>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AE5CDSHO2Q6ZJYQ3FNYQBB3PSBYQBANCNFSM4HHZB32Q>
.
@n3rd253 your issue is unrelated to this Gist. I would check that you are importing your service correctly where you are using it and make sure it is exported correctly from the module where it is defined. Not sure how much I can help you there.
@seansean11
It ended up being that I needed to declare as below, because its a class.
Appreciate the gist and responses!! :)
const ThreeDeeMartStore = createStore(rootReducer, applyMiddleware(thunk.
withExtraArgument({ sketchfabClient: new SketchfabClient() })));
…On Wed, Apr 24, 2019 at 2:27 PM Sean Michael ***@***.***> wrote:
@n3rd253 <https://github.com/n3rd253> your issue is unrelated to this
Gist. I would check that you are importing your service correctly where you
are using it and make sure it is exported correctly from the module. Not
sure how much I can help you there.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/196c436988c1fdf4b22cde308c492fe5#gistcomment-2897593>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AE5CDSDYBURKPAZXTVA5LRLPSDGERANCNFSM4HHZB32Q>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@seansean11 Thanks mate!
I've followed your typing patterns above and in addition your mark above about withExtraArgument, however when run, i'm still seeing:
FetchCategories.tsx:9 Uncaught (in promise) TypeError: Cannot read property 'getCategories' of undefined
at _callee2$ (FetchCategories.tsx:9)
at tryCatch (runtime.js:45)
at Generator.invoke [as _invoke] (runtime.js:271)
at Generator.prototype.(:9999/anonymous function) [as next] (http://localhost:9999/bundle.js:744:21)
at asyncGeneratorStep (asyncToGenerator.js:3)
at _next (asyncToGenerator.js:25)
at asyncToGenerator.js:32
at new Promise ()
at asyncToGenerator.js:21
at FetchCategories.tsx:7
Hopefully you'll be willing to help me find my typing miss?