J
J
J. Snow2020-01-14 12:44:58
Web development
J. Snow, 2020-01-14 12:44:58

How to set a static property to a functional component when using forwardRef?

I am writing an application in React + TypeScript.
There is one functional component wrapped in forwardRef.
How to add a static property to it?
Here's what I already tried:

export const MyComp = forwardRef(({}, ref: Ref<any>) => <div/>)

MyComp.myStaticProp = 123
// TS2339: Propety 'myStaticProp' does not exist on type 'ForwardRefExoticComponent< RefAttribute5>'

export const MyComp: ForwardRefExoticComponent<{}> & {myStaticProp: number} = forwardRef(({}, ref: Ref<any>) => <div/>)
// Type ... not assignable. 'myStaticProp' is missing.

MyComp.myStaticProp = 123

export const MyComp = forwardRef(({}, ref: Ref<any>) => <div/>) as ForwardRefExoticComponent<{}> & {myStaticProp: number}

MyComp.myStaticProp = 123

// Тут всё ок, но внешний код почему-то не понимает, что у 'MyComp' есть 'ref'.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lev Zabudkin, 2020-01-15
@zabudkin

I think it's all about <div/>;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question