简体   繁体   中英

Spread props type in React

I was creating a react component for icon.

The component looks like this

import React from "react";
import MIcon from "react-native-vector-icons/MaterialIcons";
import MaterialIconCommunity from "react-native-vector-icons/MaterialCommunityIcons";
import icons from "./names";

interface Props { 
  name: string
}
const Icon = ({ name, ...props }: Props) => {
  if (icons[name] === "material-community") {
    return <MaterialIconCommunity name={name} {...props} />;
  }
  if (icons[name] === "material") {
    return <MIcon name={name} {...props} />;
  }

What should be the type of ...props here?

You are extending a component from a library. If that library provides types, you should be able to use them and extend with your additional props (in this case, just "name"). You should inspect the source file for MaterialIconCommunity and MIcon , but it would look something like this:

interface Props extends SomeMaterialIconPropsType { 
  name: string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM