reactjs - 属性值预期的字符串类型,但为空
问题描述
我有一个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>
)
};
还有一个“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
但我无法弄清楚,任何帮助将不胜感激。
解决方案
问题出在ES6
语法上,效果很好:
export default function Icon({ type }) {
return (
<div>
<img src={iconSrc[type]} />
</div>
)
};
推荐阅读
- angular - 在条件运算符(三元)上使用 Angular i18n
- node.js - 根据团队规模/数量更改 Stripe 中的定价层
- java - 按下按钮 FXML 后从 Textfield 获取文本
- lucene - 我们可以使用 Google Translate API 来索引文件以进行搜索吗?
- error-handling - 自定义错误的 Mendix 错误处理
- html - NodeJS Post错误,没有表单操作
- c++ - C++ SNAP 库将 std::string 转换为 TStr
- c++ - C++ 中的时间测量(gettimeofday)给出重复的结果
- javascript - 如果没有任何参数,我的平均函数会返回 nan 。我怎样才能让它返回 0 呢?
- android - 禁用上下文菜单后无法在 EditText 中选择文本