[英]want to add properties to state object in react router with typescript
I render a wall of cards, when the user clicks on a card i redirect him via react router to the card and render just this specific card.我渲染了一堵卡片,当用户点击卡片时,我通过反应路由器将他重定向到卡片并只渲染这张特定的卡片。 To do so i need to pass the props between the components.
为此,我需要在组件之间传递道具。 I created a component which either renders with a NavLink(wall element) and without (solo Card);
我创建了一个组件,它可以使用 NavLink(墙元素)呈现,也可以不使用(单卡);
To pass the props i decided to use the react router state object.This works but because TS loves me so much it tells me, that its possibly not a good idea to do so,because the properties in state could be undefined.为了传递道具,我决定使用反应路由器 state object。这可行,但因为 TS 非常爱我,它告诉我,这样做可能不是一个好主意,因为 Z9ED39E2EA931586B6A985A6 中的属性可能未定义。
So its clear i need to tell TS that those properties exist.所以很明显,我需要告诉 TS 这些属性存在。 I created a interface Match but dont know where to add it.
我创建了一个接口 Match 但不知道在哪里添加它。
Its 1 component which eiher a wall or card element.它是墙壁或卡片元素的 1 个组件。
import {memo} from "react";
import {Picture} from "./../Picture/Picture";
import {NavLink} from "react-router-dom";
import {
RouteComponentProps,
StaticRouterProps,
} from "react-router-dom";
import "./card.scss";
interface CompProps
extends Partial<RouteComponentProps> {
userId?: string;
id?: number;
title?: string;
body?: string;
userName?: string;
clItem: string;
cardLink: string;
clTitle: string;
clBody: string;
clUser: string;
linkOptional?: boolean;
}
// interface Match {
// location: {
// state: {
// userId: string;
// body: string;
// title: string;
// };
// };
// }
export const Card: React.FC<CompProps> = memo(
({
userId,
id,
title,
body,
clItem,
cardLink,
clTitle,
clBody,
clUser,
userName,
match,
history,
linkOptional,
}): JSX.Element => {
return (
<>
{!linkOptional ? (
<NavLink
to={{
pathname: `/post/${id}/${userName}`,
state: {
userId: userId,
title: title,
body: body,
},
}}
className={cardLink}>
<section className={clItem}>
<p className={clUser}>{userId}</p>
<Picture
mediasize="1000"
clPicture="picture_item"
/>
<p className={clTitle}>{title}</p>
<p className={clBody}>{body}</p>
</section>
</NavLink>
) : (
<section className={clItem}>
{console.log(match, history)}
<p
className={
history.location.state.userId
}></p>
<Picture
mediasize="1000"
clPicture="picture_item"
/>
<p className={clTitle}>{title}</p>
<p className={clBody}>{body}</p>
</section>
)}
</>
);
}
);
This is the error is see:这是错误,请参见:
/media/darek/Seagate Expansion Drive/WebstormProjects/hsbc/src/components/Card/Card.tsx
TypeScript error in /media/darek/Seagate Expansion Drive/WebstormProjects/hsbc/src/components/Card/Card.tsx(80,17):
Object is possibly 'undefined'. TS2532
78 | <p
79 | className={
> 80 | history.location.state.userId
| ^
81 | }></p>
82 | <Picture
83 | mediasize="1000"
i resolved it by using the useLocation hook;我通过使用 useLocation 挂钩解决了它;
**interface Match {
userId: string;
body: string;
title: string;
}**
interface CompProps {
clItem: string;
cardSolo: string;
clTitle: string;
clBody: string;
clUser: string;
}
export const CardNoLink: React.FC<CompProps> = ({
clItem,
cardSolo,
clTitle,
clBody,
clUser,
}): JSX.Element => {
**const {state} = useLocation<Match>();**
return (
<section className={`${clItem} ${cardSolo}`}>
**{console.log(state.userId)}**
This allowed me to add the properties to State.这允许我将属性添加到 State。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.