Skip to content

Commit 1602854

Browse files
authored
Update render-function.md
1 parent ba899e9 commit 1602854

File tree

1 file changed

+44
-62
lines changed

1 file changed

+44
-62
lines changed

src/guide/extras/render-function.md

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -730,95 +730,77 @@ MyComponent.inheritAttrs = false
730730

731731
Functional components can be registered and consumed just like normal components. If you pass a function as the first argument to `h()`, it will be treated as a functional component.
732732

733-
### Typing Functional Components
733+
### Typing Functional Components<sup class="vt-badge ts" /> {#typing-functional-components}
734734

735-
Functional Components can be typed based on whether they are named or anonymous. Volar will warn you if you are not using the right props. It will show you the right types for events that are emitted by them if you are using the right name.
735+
Functional Components can be typed based on whether they are named or anonymous. Volar also supports type checking properly typed functional components when consuming them in SFC templates.
736736

737737
**Named Functional Component**
738738

739-
740739
```tsx
741-
import type { SetupContext } from "vue";
742-
740+
import type { SetupContext } from 'vue'
743741
type FComponentProps = {
744-
  message:string
742+
message: string
745743
}
746744

747745
type Events = {
748-
  sendMessage(message:string):void
746+
sendMessage(message: string): void
749747
}
750748

751-
function FComponent(props:FComponentProps, context:SetupContext<Events>) {
752-
753-
  return <button onClick={()=>context.emit("sendMessage", props.message)}>
754-
  {props.message}
755-
  </button>
756-
749+
function FComponent(
750+
props: FComponentProps,
751+
context: SetupContext<Events>
752+
) {
753+
return (
754+
<button onClick={() => context.emit('sendMessage', props.message)}>
755+
{props.message} {' '}
756+
</button>
757+
)
757758
}
758-
759-
FComponentProps.props={
760-
message:{
761-
type:String,
762-
required:true
763-
},
764-
}
765-
766-
FComponent.emits={
767-
sendMessage:(value)=> typeof value === "string"
759+
760+
FComponent.props = {
761+
message: {
762+
type: String,
763+
required: true
768764
}
765+
}
769766

767+
FComponent.emits = {
768+
sendMessage: (value: unknown) => typeof value === 'string'
769+
}
770770
```
771771

772-
773772
**Anonymous Functional Component**
774773

775774
```tsx
776775
import type { FunctionalComponent } from 'vue'
777776

778-
type FComponent = {
779-
  message:string
777+
type FComponentProps = {
778+
message: string
780779
}
781780

782781
type Events = {
783-
  sendMessage(message:string):void
782+
sendMessage(message: string): void
784783
}
785784

786-
const FComponent:FunctionalComponent<FComponentProps,Events> = (props, context) {
787-
788-
  return <button
789-
  onClick={()=>context.emit("sendMessage", props.message)}
790-
  >
791-
  {props.message}
792-
  </button>
793-
785+
const FComponent: FunctionalComponent<FComponentProps, Events> = (
786+
props,
787+
context
788+
) => {
789+
return (
790+
<button onClick={() => context.emit('sendMessage', props.message)}>
791+
  {props.message} {' '}
792+
</button>
793+
)
794794
}
795795

796-
FComponent.props={
797-
message:{
798-
type:String,
799-
required:true
800-
},
801-
}
802-
803-
FComponent.emits={
804-
sendMessage:(value)=> typeof value === "string"
796+
FComponent.props = {
797+
message: {
798+
type: String,
799+
required: true
805800
}
806-
807-
```
808-
809-
810-
811-
812-
813-
814-
815-
816-
817-
818-
819-
820-
821-
822-
801+
}
823802

824-
803+
FComponent.emits = {
804+
sendMessage: (value) => typeof value === 'string'
805+
}
806+
```

0 commit comments

Comments
 (0)