0

I'm currently using the Typescript version of reactjs and keep getting the error "message is not defined".

This error is coming from line 5.

import './App.css'
import * as React from "react";

const MyComponent: React.FC = ({ message: string }) => {
    return <h1>{message}</h1>
}

export default function App() {
  return (
      <div className="app">
        <MyComponent message="hello world"></MyComponent>
      </div>
    );
}
1
  • 2
    Your syntax is wrong: ({ message: string }) doesn't do what you think it does. You probably want ({ message }: { message: string }) Commented Apr 28, 2022 at 13:38

1 Answer 1

2

You may have to typecheck your component so it knows to expect message as a parameter! Please see the following post for some more context: https://fettblog.eu/typescript-react/components/. Here is a quick solution that might help!

import './App.css'
import * as React from "react";

type MyComponentProps = {
  message: string
}

const MyComponent: React.FC = ({ message }: MyComponentProps) => {
    return <h1>{message}</h1>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, thank you! For some odd reason I googled this but couldn't find the answer for this, it works now.
Yay, sounds good. I'm glad it works! Best of luck with your app.

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.