Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create custom style with React and Bootstrap

I'm using react-bootstrap NPM package to make my React components look properly. I need to customize some of them, so I'm following the official React-Bootstrap documentation, but the code throws the error index.jsx:5 Uncaught TypeError: Cannot read property 'addStyle' of undefined.

This is my custom Button component code:

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>
        );
    }
}
like image 631
JustLogin Avatar asked Mar 23 '26 15:03

JustLogin


2 Answers

Change to this:

import { bootstrapUtils } from 'react-bootstrap/lib/utils';
like image 54
Jeff McCloud Avatar answered Mar 26 '26 04:03

Jeff McCloud


You can do this as well:

    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>
            );
        }
    }
like image 39
Poiz Avatar answered Mar 26 '26 06:03

Poiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!