TS2322: Type '{ text: string; }' is not assignable to type 'string'. while passing prop
Asked Answered
V

3

5

I am trying to pass a prop of type string to a functional component defined in the same file and it is throwing the error:

TS2322: Type '{ text: string; }' is not assignable to type 'string'.

I have been trying different syntaxes, but the error comes out to be the same.

const CircleText = (text: string): JSX.Element => (
    <p>{text}</p>
)

export default function Login(): JSX.Element {
return (
    <div>
        <h1>Login</h1>
        <CircleText text="testing"/>

    </div>
);
} 
Vanwinkle answered 17/1, 2022 at 19:21 Comment(0)
S
5

Props in a react component needs to be an object, notice the {} in the first line below:

const CircleText = ({text: string}): JSX.Element => (
    <p>{text}</p>
)
Sidky answered 17/1, 2022 at 19:25 Comment(0)
V
1

Seems like this also works as well based off of https://www.pluralsight.com/guides/defining-props-in-react-function-component-with-typescript:

interface Title {
     text: string
 }
 const SemiCircleText = (props: Title): JSX.Element => (
     <p>{props.text}</p>
 )
Vanwinkle answered 17/1, 2022 at 19:35 Comment(0)
A
1
const CircleText = ({ text }: { text: string }): JSX.Element => <p>{text}</p>;

or

interface CircleTextProps {
  text: string;
}

const CircleText = ({ text }: CircleTextProps): JSX.Element => <p>{text}</p>;
Anderer answered 26/9, 2022 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.