How to enter props in react typescript?
P粉897881626
P粉897881626 2024-03-31 15:34:36
0
2
386

How to enter props in React TypeScript? I'm getting the error Property 'authors' does not exist on type '[Props] & {children?: ReactNode;'}'.ts. I'm expecting to be passed an array consisting of name and password keys.

https://codesandbox.io/s/test-login-page-tasks-24-04-forked-eg91s5?file=/src/components/Login.tsx:229-274

type Props = {
  authors: {
    name: string;
    password: string;
  }[];
};

const Login: FC<[Props]> = ({ authors }) => {
  return (
  ...
  )
}

, even though I'm passing props.

P粉897881626
P粉897881626

reply all(2)
P粉550323338

You define the IUser interface using the IUser type and define the name, password status, which will cause problems because when you define this:

const [name, setName] = useState("");
// name will expected to be 
name = {
    name: 'name',
    password: 'password'
}

This is wrong because name, password is a string, so try defining it like this:

const [name, setName] = useState("");
const [password, setPassword] = useState("");

Then you have to fix passing the author to the FC component:

const Login: FC // instead of 

The last thing you have to fix this type to match the data type passed from App.tsx:

type Props = {
    authors: {
       author_name: string; // instead of name
       password: string;
    }[];
};


// because you are passing and comparing here with author_name not with name

const userData = authors.find(
  (user) => user.author_name === name && user.password === password
);

and you pass it from your application like this:

const Authors = [
   {
      id: "001",
      author_name: "John Smith", // not a name
      password: "123"
   }
 ];
P粉903052556

Try changing [Props] to Props as shown below

const Login: FC = ({ authors }) => { ... }

Now it works because it requires an object instead of an array, we will pass an object like <Loginauthors={authors} />. Now, the author will be an array of objects.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template