javascript - React:通过对象映射值以返回 SVG
问题描述
我正在尝试从父组件(呈现标签)传递一个值,并希望很快将一个图标向下传递到我的CountryFlag
组件中。
到目前为止,它是这样通过的:
<CountryFlag
countryCode={option.code}
/>
option.code
返回一个国家代码,例如“AFN”。
然后,我想将该 countryCode 放入我的 CountryFlag 组件并映射到我的CountryMap
数组,以便例如,如果从父组件传递“AFN”,我将能够返回“Afghan”SVG 作为渲染的 FlagComponent 的一部分.
我知道我离最终的实施还有一点距离,但任何关于我下一步可能去哪里的建议都会非常感激。
import Afghanistan from "./CountryFlags/Afghanistan.svg";
import Albania from "./CountryFlags/Albania.svg"
import styled from "styled-components";
const countryMap = {
AFN: {
countryCode: "AFN",
name: "afghanistan",
icon: Afghanistan,
},
ALL: {
countryCode: "ALL",
name: "albania",
icon: Albania,
},
};
const StyledImage = styled.div`
height: 32px;
width: 32px;
`;
const CountryFlag = ({className, code="" }: Props) => {
const FlagComponent = countryMap[code];
if (!FlagComponent) {
console.warn(`icon for ${code} is not valid`);
return null;
}
return (
<StyledImage>
<FlagComponent>
src={}
</FlagComponent>
</StyledImage>
);
}
export default CountryFlag;
解决方案
推荐阅读
- aws-lambda - 如何防止 Aws Step Function 在调用 Lambda 时插入父键“Input”?
- c# - 范围服务在(非 Web)主机上下文中的含义
- powershell - 如何以安全的方式提供 api 密钥?
- php - Laravel POST 请求中收到的原始 PDF
- python - 使用 Keras.NET 进行数组整形
- android - 底部导航上的 Nativescript 设置 selectedIndex 在 Android 上不起作用
- python - 我的 pygame 屏幕的左上角没有正确更新,但其他一切正常
- python - Python正则表达式匹配序列中的许多标记
- redirect - wp_redirect() 函数导致 WooCommerce Checkout 上的无限加载/微调器
- android - 每当用户在颤动中更改操作系统主题时如何获得通知?