Reactjs 有没有办法在typescript中键入React router NavLink的isActive函数 { 如果(匹配){ onNavSelectHandler(索引); 返回true; }否则{ 返回false; } }} > {text&&isOpen&&}
我试图在typescript项目中使用react router的isActive功能,但我无法正确键入它…react router domReactjs 有没有办法在typescript中键入React router NavLink的isActive函数 { 如果(匹配){ onNavSelectHandler(索引); 返回true; }否则{ 返回false; } }} > {text&&isOpen&&},reactjs,typescript,react-router,Reactjs,Typescript,React Router,我试图在typescript项目中使用react router的isActive功能,但我无法正确键入它…react router domNavLink组件的isActive属性定义如下: <MenuItem component={NavLink} to={to} key={text} selected={selectedNavIndex === index} **// compiler
NavLink
组件的isActive
属性定义如下:
<MenuItem
component={NavLink}
to={to}
key={text}
selected={selectedNavIndex === index}
**// compiler complaining about match of any type**
isActive={match => {
if (match) {
onNavSelectHandler(index);
return true;
} else {
return false;
}
}}
>
<StyledListItemIcon>
<StyledIcon as={icon} width={width} />
</StyledListItemIcon>
{text && isOpen && <ListItemText primary={text} />}
</MenuItem>
{
如果(匹配){
onNavSelectHandler(索引);
返回true;
}否则{
返回false;
}
}}
>
感谢您的帮助,以及如何键入isActive函数的第二个参数(位置)?从“历史记录”导入{location}。泛型描述位置的状态属性。
export interface NavLinkProps<S = H.LocationState> extends LinkProps<S> {
isActive?<Params extends { [K in keyof Params]?: string }>(match: match<Params> | null, location: H.Location<S>): boolean;
...
import {NavLink, match as Match} from "react-router-dom";
<MenuItem
...
isActive={(match: Match | null) => {
if (match) {
onNavSelectHandler(index);
return true;
} else {
return false;
}
}}
>