5

How would I go about this in typescript. I know I can do it with giving it a name but thats not what I'm asking. I'm asking how can I still just use default.

interface IProps {
  name: string,
  tag: string,
}

export default ({name, tag}: IProps) => {
 ..... my code 
});

Yes I know about this.

const MyComponent: React.FunctionComponent = () => {
...
export default MyComponent;

1 Answer 1

3

Um, exactly like you have it, except for the trailing ) syntax error

import React from "react"

interface IProps {
  name: string,
  tag: string,
}

export default ({name, tag}: IProps): JSX.Element => {
  return <p>hello { name }, some { tag }</p>
} // <- no trailing `)`

Or -

import React from "react"

interface IProps {
  name: string,
  tag: string,
}

export default ({name, tag}: IProps): JSX.Element =>
  <p>hello { name }, some { tag }</p>

TypeScript Playground Demo

That said, a named function is much better, especially for debugging purposes.

Sign up to request clarification or add additional context in comments.

3 Comments

So you wouldn't use React.FunctionComponent instead of JSX.Element ? Thanks for the help :)
@me-me Yep, a functional component is just one that accepts props as an object, and returns JSX compatible content. All you need to do is conform to that expectation.
@me-me, there's a subtle difference using React.FunctionComponent. I personally don't see the gain unless you are trying to enforce a very specific constraint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.