8

When trying to use the new useSelector hook (see below example) of react-redux typescript gives an error that the function does not exist:

Module '"../../../node_modules/@types/react-redux"' has no exported member 'useSelector'.  TS2305

Example:

import * as React from "react"
import { useSelector } from "react-redux"
import { Message } from "./Message"

export const MessageContainer = () => {
  const searchValue = useSelector((state) => state.search)
  return (
    <Message searchValue={searchValue} />
  )
}

Used versions: "react-redux": "^7.1.0-alpha.5" "@types/react-redux": "^7.0.9"

3
  • 4
    Typescript has not updated yet. You're using @types/react-redux which has 7.0.9 version. These hooks were added in 7.1.0. Commented May 20, 2019 at 15:21
  • Since it's currently the latest version of the types, is there any workaround to import the function and avoid the error? Commented May 20, 2019 at 15:32
  • 1
    Apparently a temporary fix can be done by adding the module definition yourself: github.com/DefinitelyTyped/DefinitelyTyped/pull/… Commented May 21, 2019 at 10:05

2 Answers 2

0

the code below took from redux doc itself about typescript
typescript redux offical

interface RootState {
      isOn: boolean
    }

// TS infers type: (state: RootState) => boolean
const selectIsOn = (state: RootState) => state.isOn

// TS infers `isOn` is boolean
const isOn = useSelector(selectIsOn)

as it says before the hooks been added later but you better write Selector like this if you use the hooks

Sign up to request clarification or add additional context in comments.

Comments

0

As of now, your react-redux version is old. useSelector was added in 7.1.0 stable. You can fix now by upgrading both packages.

npm install react-redux@^7.2.0 @types/react-redux@^7.1.9

The useSelector should work with typescript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.