-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add example for action types in connected components using typeof #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||
| import React from 'react'; | ||||||
| import { connect } from 'react-redux'; | ||||||
| import { createAction } from '@reduxjs/toolkit'; | ||||||
| import { Button } from 'react-native'; | ||||||
|
|
||||||
| // Define the actions | ||||||
| const actions = { | ||||||
| ping: createAction<number>('ping/PING', (arg: number) => ({ | ||||||
| payload: { arg }, | ||||||
| })), | ||||||
| }; | ||||||
|
|
||||||
| // Define the props interface using typeof | ||||||
| interface Props { | ||||||
| ping: typeof actions.ping; | ||||||
| } | ||||||
|
|
||||||
| // Define the functional component | ||||||
| const PingTestComponent: React.FC<Props> = ({ ping }) => { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve testability, it's a good practice to export the unconnected
Suggested change
|
||||||
| return ( | ||||||
| <Button onPress={() => ping(123)} title="Ping" /> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| // Connect the component to the Redux store | ||||||
| export const PingTest = connect( | ||||||
| null, | ||||||
| { ping: actions.ping } | ||||||
| )(PingTestComponent); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
createActioncall, the type annotation for theargparameter is redundant. Since you've specified<number>as the generic argument tocreateAction, TypeScript can correctly infer thatargis of typenumber. Removing the explicit type annotation makes the code more concise.