Skip to content

Commit

Permalink
Merge pull request #281 from estaub/patch-2
Browse files Browse the repository at this point in the history
Describe use of generic components with JSX
  • Loading branch information
basarat authored May 20, 2017
2 parents 80296d3 + a6b2b89 commit 3fcc47a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/jsx/tsx.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ const bar: React.ReactElement<MyAwesomeComponent> = <NotMyAwesomeComponent/>; //

> Of course you can use this as a function argument annotation and even React component prop member.

### React JSX Tip: Generic components
There's no syntax in JSX to apply generic parameters to a generic component; a type and constructor must first be created in Typescript proper. Example:

```ts
/** A generic component */
type SelectProps<T> = { items: T[] }
class Select<T> extends React.Component<SelectProps<T>, any> { }

/** Specialize Select to use with strings */
interface IStringSelect { new (): Select<string> } ;
/** Constructor function - must be capitalized for JSX
const StringSelect = Select as IStringSelect;
/** Usage */
const Form = ()=> <StringSelect items={['a','b']} />;
```
## Non React JSX
TypeScript provides you with the ability to use something other than React with JSX in a type safe manner. The following lists the customizability points, but note that this is for advanced UI framework authors:
Expand Down

0 comments on commit 3fcc47a

Please sign in to comment.