代码之家  ›  专栏  ›  技术社区  ›  CCCC

反应-材质UI图标的类型应该是什么

  •  2
  • CCCC  · 技术社区  · 5 年前

    我将把MUI图标传递给组件 Test 我想为图标创建Protype,但我不确定图标的正确Protype是什么

    App.js

    import "./styles.css";
    import VisibilityIcon from "@material-ui/icons/Visibility";
    
    import Test from "./Test";
    
    export default function App() {
      return (
        <div className="App">
          <Test icon={<VisibilityIcon />} />
        </div>
      );
    }
    

    import PropTypes from "prop-types";
    
    function Test(props) {
      const { icon } = props;
      return (
        <div>
          Icon:
          {icon}
        </div>
      );
    }
    
    Test.propTypes = {
      // icon: ??????
    };
    
    export default Test;
    

    https://codesandbox.io/s/intelligent-curie-8mtbw?file=/src/Test.jsx:0-213

    2 回复  |  直到 5 年前
        1
  •  2
  •   Ryan Le    5 年前

    它应该是一个 PropTypes.element

    Test.propTypes = {
      icon: PropTypes.element
    };
    

    工作示例:

    Edit fervent-poitras-ubr96

        2
  •  2
  •   Drew Reese    5 年前

    我会和你一起去 PropTypes.element

    Typechecking with PropTypes

    Test.propTypes = {
      icon: PropTypes.element,
    };
    
    // A React element.
    optionalElement: PropTypes.element,
    

    如果你想更宽松一点,那么使用 PropTypes.node 对于任何可渲染的东西。

    // Anything that can be rendered: numbers, strings, elements or an array
    // (or fragment) containing these types.
    optionalNode: PropTypes.node,
    

    Edit react-what-should-be-the-proptype-of-material-ui-icon