2

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>
        );
    }
}
2
  • your bootstrapUtils object is not there, make sure your have the correct path to the react-bootstrap package, are you packaging this with webpack or something ? if so you need to do extra steps to make it available in your components class declarations
    – ZEE
    Commented Oct 28, 2016 at 12:00
  • I've checked the path, and bootstrapUtils file is there. It also has addStyle function in exports. Also, I'm using Webpack.
    – JustLogin
    Commented Oct 28, 2016 at 12:01

3 Answers 3

6

Change to this:

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

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>
            );
        }
    }
2
  • Can I store my styles in .css, or I have to declare them in JS?
    – JustLogin
    Commented Oct 28, 2016 at 12:33
  • How the code above can be refactored to store styles in .css file?
    – JustLogin
    Commented Oct 28, 2016 at 13:33
0

bootstrapUtils is a named export and not a default therefore you need to use {} around it.

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

1
  • Geraint was almost right. You have to remove the "/bootstrapUtils" from the path. See my answer Commented Oct 28, 2016 at 20:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.