Telegram Group & Telegram Channel
Как использовать forwardRef с generic компонентами

Статья объясняет, как использовать forwardRef с обобщенными компонентами в TypeScript. Она рассматривает проблемы forwardRef с типизацией обобщенных компонентов и предлагает способ их решения. Этот метод позволяет сохранить типовую безопасность при передаче ссылок на компоненты.

Одно из ограничений forwardRef в том, что он отключает выведение типа для generic компонентов. Например:


const Table = <T,>(
props: {
data: T[];
renderRow: (row: T) => React.ReactNode;
},
ref: React.ForwardedRef<HTMLTableElement>
) => {
/** --snip-- */
};

const ForwardReffedTable = React.forwardRef(Table);

<Table
data={["a", "b"]}
renderRow={(row) => { // Тип выводится: row: string
return <tr>{row}</tr>;
}}
/>;

<ForwardReffedTable
data={["a", "b"]}
renderRow={(row) => { // Тип не выводится: row: unknown
return <tr>{row}</tr>;
}}
/>;


https://www.totaltypescript.com/forwardref-with-generic-components

✍️ @React_lib



tg-me.com/React_lib/619
Create:
Last Update:

Как использовать forwardRef с generic компонентами

Статья объясняет, как использовать forwardRef с обобщенными компонентами в TypeScript. Она рассматривает проблемы forwardRef с типизацией обобщенных компонентов и предлагает способ их решения. Этот метод позволяет сохранить типовую безопасность при передаче ссылок на компоненты.

Одно из ограничений forwardRef в том, что он отключает выведение типа для generic компонентов. Например:


const Table = <T,>(
props: {
data: T[];
renderRow: (row: T) => React.ReactNode;
},
ref: React.ForwardedRef<HTMLTableElement>
) => {
/** --snip-- */
};

const ForwardReffedTable = React.forwardRef(Table);

<Table
data={["a", "b"]}
renderRow={(row) => { // Тип выводится: row: string
return <tr>{row}</tr>;
}}
/>;

<ForwardReffedTable
data={["a", "b"]}
renderRow={(row) => { // Тип не выводится: row: unknown
return <tr>{row}</tr>;
}}
/>;


https://www.totaltypescript.com/forwardref-with-generic-components

✍️ @React_lib

BY React




Share with your friend now:
tg-me.com/React_lib/619

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

NEWS: Telegram supports Facetime video calls NOW!

Secure video calling is in high demand. As an alternative to Zoom, many people are using end-to-end encrypted apps such as WhatsApp, FaceTime or Signal to speak to friends and family face-to-face since coronavirus lockdowns started to take place across the world. There’s another option—secure communications app Telegram just added video calling to its feature set, available on both iOS and Android. The new feature is also super secure—like Signal and WhatsApp and unlike Zoom (yet), video calls will be end-to-end encrypted.

How to Use Bitcoin?

n the U.S. people generally use Bitcoin as an alternative investment, helping diversify a portfolio apart from stocks and bonds. You can also use Bitcoin to make purchases, but the number of vendors that accept the cryptocurrency is still limited. Big companies that accept Bitcoin include Overstock, AT&T and Twitch. You may also find that some small local retailers or certain websites take Bitcoin, but you’ll have to do some digging. That said, PayPal has announced that it will enable cryptocurrency as a funding source for purchases this year, financing purchases by automatically converting crypto holdings to fiat currency for users. “They have 346 million users and they’re connected to 26 million merchants,” says Spencer Montgomery, founder of Uinta Crypto Consulting. “It’s huge.”

telegram from de


Telegram React
FROM USA