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粉5503233382024-04-01 15:15:04
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 <[Props]>
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粉9030525562024-04-01 12:59:05
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.