无法使用 React 和 Bootstrap 创建自定义样式

Cannot create custom style with React and Bootstrap

我正在使用 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>
        );
    }
}

你也可以这样做:

    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() {
            var styles={
                "backgroundColor" : "purple",
                "color"           : "white"
            };
            return (
                <div>
                    <Button style={styles} bsStyle="primary">Custom</Button>
                </div>
            );
        }
    }

bootstrapUtils 是一个命名导出,而不是默认导出,因此您需要在其周围使用 {}

尝试使用: import { bootstrapUtils } from 'react-bootstrap/lib/utils/bootstrapUtils';

改为:

import { bootstrapUtils } from 'react-bootstrap/lib/utils';