Ad
React.memo Is Not Available When Working With Typescript?
I'm trying to use React.memo like so:
const Node: React.memo<Props> = props => {
But i'm getting an error saying:
Namespace '../ui/node_modules/@types/react/index.d.ts"' has no exported member 'memo'.
But I can see the Memo function within index.d.ts:
function memo<P extends object>(
Component: SFC<P>,
propsAreEqual?: (prevProps: Readonly<PropsWithChildren<P>>, nextProps: Readonly<PropsWithChildren<P>>) => boolean
): NamedExoticComponent<P>;
function memo<T extends ComponentType<any>>(
Component: T,
propsAreEqual?: (prevProps: Readonly<ComponentProps<T>>, nextProps: Readonly<ComponentProps<T>>) => boolean
): MemoExoticComponent<T>;
What am I missing here?
Ad
Answer
As mentioned in the comments, memo
is not a type declaration. You have your syntax messed up a little bit
const Node: React.FC<Props> = React.memo(props => {
return(
<div>
memoized
</div>
)
})
Note that React.FC
is a declaration of a functional component. Since you're wrapping it in a memo
, you might need to change the type. But personally, I have not needed to.
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad