【发布时间】:2017-03-11 07:05:04
【问题描述】:
我正在使用react-bootstrap NPM 包来使我的React 组件看起来正确。我需要自定义其中一些,所以我关注official React-Bootstrap documentation,但是代码抛出了错误index.jsx:5 Uncaught TypeError: Cannot read property 'addStyle' of undefined。
这是我自定义的Button组件代码:
import React from "react";
import Button from 'react-bootstrap/lib/Button';
import bootstrapUtils from 'react-bootstrap/lib/utils/bootstrapUtils';
bootstrapUtils.addStyle(Button, 'custom');
export default class MediaItemButton extends React.Component {
render() {
return (
<div>
<style type="text/css">{`
.btn-custom {
background-color: purple;
color: white;
}
`}</style>
<Button bsStyle="primary">Custom</Button>
</div>
);
}
}
【问题讨论】:
-
你的 bootstrapUtils 对象不存在,确保你有正确的 react-bootstrap 包路径,你是用 webpack 还是其他东西打包它?如果是这样,您需要执行额外的步骤以使其在组件类声明中可用
-
我已经检查了路径,并且 bootstrapUtils 文件在那里。它还在导出中具有 addStyle 功能。另外,我正在使用 Webpack。
标签: javascript css twitter-bootstrap reactjs react-bootstrap