Skip to content
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

update useSelector() ts example in API Reference #2153

Closed
wants to merge 1 commit into from
Closed

update useSelector() ts example in API Reference #2153

wants to merge 1 commit into from

Conversation

MayCXC
Copy link

@MayCXC MayCXC commented Apr 3, 2024

The Hooks API Reference provides a simplified typescript definition for useSelector() that defines a type alias for SelectorFn, but then uses SelectorFunction in place of that type later in the example. This PR replaces the SelectorFunction usage with SelectorFn, and adds a generic type parameter to EqualityFn to resemble the definition of SelectorFn more closely, and updates the name of the equalityFn parameter to equalityFnOrOptions to match its current name in v8.

The example also puts an assignment statement const result: Selected = before a function declaration for useSelector, which is technically invalid typescript. Its meaning is clear, but it does not match how other hook API functions with parameters like createStoreHook, createDispatchHook, and createSelectorHook are introduced in the docs. With this in mind, it may be better to provide the type definition for useSelector in the example, and put the assignment statement in an inline comment instead:

type RootState = ReturnType<typeof store.getState>
type SelectorFn = <Selected>(state: RootState) => Selected
type EqualityFn = <Selected>(a: Selected, b: Selected) => boolean
export type DevModeCheckFrequency = 'never' | 'once' | 'always'

interface UseSelectorOptions {
  equalityFn?: EqualityFn
  devModeChecks?: {
    stabilityCheck?: DevModeCheckFrequency
    identityFunctionCheck?: DevModeCheckFrequency
  }
}

export const useSelector: <Selected>(
  selector: SelectorFn,
  equalityFnOrOptions?: EqualityFn | UseSelectorOptions
) => Selected

// Example usage:
// const result: Selected = useSelector(selector, equalityFnOrOptions)

Issue: #2152

Copy link

codesandbox-ci bot commented Apr 3, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

netlify bot commented Apr 3, 2024

Deploy Preview for react-redux-docs ready!

Name Link
🔨 Latest commit 85b8b8c
🔍 Latest deploy log https://app.netlify.com/sites/react-redux-docs/deploys/660d81856b41ba00086e136d
😎 Deploy Preview https://deploy-preview-2153--react-redux-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@MayCXC MayCXC changed the title update useSelector() ts example update useSelector() ts example in API Reference Apr 3, 2024
@timdorr timdorr closed this Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants