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

属性值应为字符串类型,但为空

  •  0
  • user3378165  · 技术社区  · 7 年前

    我有一个 Icon.jsx 组成部分:

    import React from 'react'; import { css } from 'emotion';
    
    import ArrowRight from "./arrow-right.svg";
    
    export const iconTypes = {
        arrowRight: 'ARROW_RIGHT',
        //arrowLeft: "ARROW_LEFT", }
    
    const iconSrc = {
        ARROW_RIGHT: ArrowRight,
        ARROW_LEFT: ArrowLeft, }
    
    
    export default ({ type }) => {
        return (
            <Icon>
                <img src={iconSrc[type]} />
            </Icon>
        )
    };
    

    和一个`图标.jsx'故事:

    import React from "react";
    import { storiesOf } from "@storybook/react";
    import { action } from "@storybook/addon-actions";
    import Icon from "../components/Icon/Index";
    import { iconTypes } from "../components/Icon/Index";
    
    
    storiesOf("Icon", module)
        .add("with text", () => (
            <Icon type={iconTypes.leftArrow}>
                </Icon>
        ));
    

    我经常在计算机上遇到以下错误 Icon.jxs

     Property value expected type of string but got null
    

    但我想不出来,任何帮助都将不胜感激。

    2 回复  |  直到 7 年前
        1
  •  1
  •   user3378165    7 年前

    ES6 语法,很好用:

    export default function Icon({ type }) {
        return (
            <div>
                <img src={iconSrc[type]} />
            </div>
        )
    };
    
        2
  •  0
  •   Bhojendra Rauniyar    7 年前

    <Icon type={iconTypes.leftArrow}></Icon> 没有定义,因此 type={undefined} 传递undefined或null将导致指定的错误。

    <Icon type={iconTypes.ARROW_LEFT}></Icon>