简体   繁体   English

如何为 props 中传递的对象定义 TypeScript 接口?

[英]How to define TypeScript interface for the passed object in props?

Could I get some help please with the definition of an interface for the IconArr.primary property.我能否就IconArr.primary属性的接口定义获得一些帮助。

I am trying to pass it into PrimarySkills components --> where I have to define interface but without any luck so far.我正在尝试将它传递给PrimarySkills组件 --> 我必须在其中定义接口但到目前为止没有任何运气。

I do not want to pass the whole object with both primary and secondary icons, so I can have two separate components but with different input data later.我不想同时传递带有主图标和辅助图标的整个对象,因此我可以有两个单独的组件,但稍后可以使用不同的输入数据。

const iconsArr = {
    primary: [
        {
            id: '0',
            imgPath: 'images/techIcons/reactjs.svg',
            name: 'React',
        },
        {
            id: '1',
            imgPath: 'images/techIcons/nextjs.svg',
            name: 'NextJS',
        },
    ],
    secondary: [
        {
            id: '0',
            imgPath: 'images/techIcons/csharp.svg',
            name: 'C#',
        },
        {
            id: '1',
            imgPath: 'images/techIcons/java.svg',
            name: 'Java',
        },
    ],
};

const Skills = (props: Props) => {
    return (
        <div>
            <StyledCaption contentString='SKILLS'>PRIMARY</StyledCaption>
            <PrimarySkills iconsArr={iconsArr.primary} />
        </div>
    );
};

in PrimarySkills ComponentPrimarySkills组件中

interface Props {
    iconsArr: {
        primary: {
            id: string;
            imgPath: string;
            name: string;
        }[];
    };
}

const PrimarySkills = (props: Props) => {
    return (
        <div>
            {props.iconsArr.primary.map((icon) => (
                <img src={icon.imgPath} alt={icon.name} key={icon.id} height='30' />
            ))}
        </div>
    );
};

You can create an interface for each item contained in the array (let's say Icon ), then use it to properly type the iconsArr prop as an array of that type ( Icon[] ).您可以为数组中包含的每个项目创建一个接口(比如Icon ),然后使用它正确地将iconsArr键入为该类型的数组( Icon[] )。

interface Icon {
    id: string;
    imgPath: string;
    name: string;
}

interface Props {
    iconsArr: Icon[]
}

const PrimarySkills = (props: Props) => {
    return (
        <div>
            {props.iconsArr.map((icon) => (
                <img src={icon.imgPath} alt={icon.name} key={icon.id} height='30' />
            ))}
        </div>
    );
};

Note that because you're passing iconsArr.primary to the iconsArr prop, iconsArr will be an array (it won't have the primary property).请注意,因为您将iconsArr.primary传递给iconsArriconsArr将是一个数组(它不会具有primary属性)。 You should map through it using props.iconsArr.map(...) instead.您应该使用props.iconsArr.map(...)来映射它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用传递的 state/setState 值定义 props 接口? - How to define props interface with passed state/setState values? 如何定义这个接口的Props,让这个Props包含传入组件的所有属性 - How to define this interface Props so that this Props contains all the attributes of the passed component React - 如何使用打字稿定义道具 - React - How to define props with typescript 如何将TypeScript包含一个包含对象的props接口并与defaultProps合并 - How to TypeScript a props interface containing an object and merge with defaultProps 如何为React props定义TypeScript类型,只有在将prop A传递给组件时,才会接受prop B? - How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component? 道具应该包含一个接口 - 如何定义它 - Props should contain an interface - how to define that 如何使用条件道具接口定义 React 组件? - How to define React component with conditional props interface? 如何在 TypeScript 中定义接口以正确传递道具? - How to define interfaces in TypeScript to pass the props correctly? React - 如何访问传递的道具 object 名称 - React - How to access passed props object name 如何在React类上定义函数的TypeScript接口? - How to define an TypeScript interface of an function on a react class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM