Skip to content

Commit 1f0e45d

Browse files
Fix issues in TSX section.
Co-authored-by: Claudia Meadows <[email protected]>
1 parent 3c5e9e8 commit 1f0e45d

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

docs/jsx.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ function ChildComponent(vNode: Vnode<Attributes>): m.Component<Attributes> {
281281

282282
function ParentComponent() {
283283
return {
284-
view: <div><ChildComponent greet="Hello World"/></div> //This line will compile correctly but shows the errors bellow
284+
view: () => <div>
285+
<ChildComponent greet="Hello World"/>
286+
</div>
285287
};
286288
}
287289
```
@@ -293,17 +295,15 @@ TS2739: Type { greet: string; } is missing the following properties from type Vn
293295
TS2786: ChildComponent cannot be used as a JSX component.
294296
```
295297

296-
There are several options to circumvent that problem:
298+
There are a few options to circumvent that problem:
297299
1. Instead of `<div><ChildComponent greet="Hello World"/></div>`, use [hyperscript](hyperscript.md) instead: `<div>{m(ChildComponent, {greet: "Hello World"})}</div>`.
298300
2. Use [class components](components.md#class-component-state) instead. Class components will not show any errors. But TypeScript will not be able to autocomplete or inspect attributes (in this example `greet` would be unknown when used in `ParentComponent`).
299-
3. Create a "translation function" (see `TsClosureComponent()` in the example below) to trick TypeScript.
301+
3. Create a "translation function" (like `TsClosureComponent()` in the example below) to trick TypeScript.
300302

301303
The following code will work without errors:
302304

303305
```tsx
304-
/**
305-
* Use TsClosureComponent to create closure components that can be inspected by TypeScript.
306-
*/
306+
// Use this helper to force TypeScript to treat closure components as valid JSX components
307307
export function TsClosureComponent<T>(create: Mithril.ClosureComponent<T>) {
308308
return create as any as (
309309
(attrs: T & Mithril.CommonAttributes<T, unknown>) => JSX.Element
@@ -331,14 +331,14 @@ function ParentComponent() {
331331

332332
```
333333

334-
When you need generics for your closure component, you can use the following definition style:
334+
This also works with generics, as long as you define the generic as part of the wrapped component:
335335

336336
```typescript jsx
337337
function ChildComponentImpl<T>() {
338338
// ...
339339
}
340340

341-
const ChildComponent = TsClosureComponent(ChildComponentImpl); //for TsClosureComponent, see above
341+
const ChildComponent = TsClosureComponent(ChildComponentImpl);
342342

343343
const jsx = <div>
344344
<ChildComponent<SomeClass> />

0 commit comments

Comments
 (0)