英文:
React remove last url from anchor inside li
问题
I want to remove the last url of an anchor tag (href="") inside a list in React.
This is the code that I have:
const Link = ({ ...props }) => <TranslationsLink {...props} />;
const Navbar: FC<Props> = () => {
return (
<Nav>
<List>
{list.map((el, i) => {
const lastEl = i === list.length - 1;
return (
<Item key={el.route}>
<Link
lastEl={lastEl}
>
{el.text}
</Link>
</Item>
);
})}
</List>
</Nav>
);
};
const TranslationsLink: FC<ITranslationsLink> = memo(({ ...props }) => {
const { findRouteByName, activeLocale } = useRouter();
const routeMatch = findRouteByName(route, locale || activeLocale);
const url = routeMatch.getUrl(params);
const nextLink = routeMatch.getNextLink(params);
const link = (
<a href={url} {...props}>
{children}
</a>
);
return <Link href={nextLink} as={url} children={link} />;
});
I tried passing the lastEl to the Link component so I can override its href:
<Link href={props.lastEl ? '' : nextLink}>
The problem is that because that same link it's being used in other places I'm getting false... How can I avoid that without creating another Link component for the last element?
英文:
I want to remove the last url of an anchor tag (href="") inside a list in React.
This is the code that I have:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const Link = ({ ...props }) => <TranslationsLink {...props} />;
const Navbar: FC<Props> = () => {
return (
<Nav>
<List>
{list.map((el, i) => {
const lastEl = i === list.length - 1;
return (
<Item key={el.route}>
<Link
lastEl={lastEl}
>
{el.text}
</Link>
</Item>
);
})}
</List>
</Nav>
);
};
const TranslationsLink: FC<ITranslationsLink> = memo(({ ...props }) => {
const { findRouteByName, activeLocale } = useRouter();
const routeMatch = findRouteByName(route, locale || activeLocale);
const url = routeMatch.getUrl(params);
const nextLink = routeMatch.getNextLink(params);
const link = (
<a href={url} {...props}>
{children}
</a>
);
return <Link href={nextLink} as={url} children={link} />;
});
<!-- end snippet -->
I tried passing the lastEl to the Link component so I can override it's href:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<Link href={props.lastEl ? '' : nextLink}
<!-- end snippet -->
The problem is that because that same link it's being used in other places I'm getting false... How can I avoid that without creating another Link component for the last element?
答案1
得分: 1
你可以像这样给你的地图添加条件:
const Navbar: FC<Props> = () => {
return (
<Nav>
<List>
{list.map((el, i) => {
const lastEl = i === list.length - 1;
return lastEl ? null : // 在这里检查最后一个元素
(<Item key={el.route}>
<Link
lastEl={lastEl}
>
{el.text}
</Link>
</Item>
);
})}
</List>
</Nav>
);
};
请注意,这只是代码的一部分,所以我只翻译了这部分内容。
英文:
You can add condition to your map like this:
const Navbar: FC<Props> = () => {
return (
<Nav>
<List>
{list.map((el, i) => {
const lastEl = i === list.length - 1;
return lastEl ? null : // just check for the lastEl here
(<Item key={el.route}>
<Link
lastEl={lastEl}
>
{el.text}
</Link>
</Item>
);
})}
</List>
</Nav>
);
};
答案2
得分: 0
我找到了答案:
const Navbar: FC<Props> = () => {
return (
<Nav>
<List>
{list.map((el, i) => {
const lastEl = i === list.length - 1;
return (
<Item key={el.route}>
{lastEl ? (
<span>{el.text}</span>
) : (
<Link>{el.text}</Link>
)}
</Item>
);
})}
</List>
</Nav>
);
};
英文:
I found the answer:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const Navbar: FC<Props> = () => {
return (
<Nav>
<List>
{list.map((el, i) => {
const lastEl = i === list.length - 1;
return
(<Item key={el.route}>
{lastEl ? (
<span>{el.text}</span>
) : (
<Link>
{el.text}
</Link>
)}
</Item>
);
})}
</List>
</Nav>
);
};
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论