I
I
ivan05122020-01-22 23:18:03
typescript
ivan0512, 2020-01-22 23:18:03

Is it possible to specify which components can be passed to children?

Tell me, can I somehow specify which components can be passed to children.
Or is it only left to use the type children: React.ReactElement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2020-02-12
@miraage

Anything can be passed to children. The question is how the component will use it.
For example, in the Render Props pattern, children are passed as a function.
// edit-response to comment
Somehow it should work.

import React from 'react';

interface MyComponentProps {
  children: (items: string[]) => void;
  regexp: RegExp;
  items: string[];
}

export const MyComponent: React.FC<MyComponentProps> = ({ children, items, regexp }) => {
  return children(items.filter(item => regexp.test(item)));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question