1

I'm new to React and started exploring bootstrap few days ago. But, whenever I'm trying to import something from react-bootstrap, it's throwing error.

I've already tried reinstalling react-bootstrap. But it doesn't solve the problem. These are my imports:

import React, { Component } from 'react';
import {Button} from 'react-bootstrap/Button'; <-- culprit
import logo from './logo.svg';
import './App.css';
import Chart from './components/Chart';
import axios from 'axios';
import {Typeahead} from 'react-bootstrap-typeahead'; <-- works fine

The error is in some line in the ThemeProvider.js, which comes with react-bootstrap.

TypeError: Object doesn't support property or method 'createContext'

  15 | 
  16 | var _react = _interopRequireWildcard(require("react"));
  17 | 
> 18 | var ThemeContext = _react.default.createContext({});
  19 | 
  20 | var Consumer = ThemeContext.Consumer,
  21 |     Provider = ThemeContext.Provider;
0

3 Answers 3

3

When you import something wrapped around with {}, it refers to something that is exported with an explicit name identifier.

In this case: import {Button} from 'react-bootstrap/Button' would mean that file has explicitly named one of their exports Button. But that's unlikely, because conventionally with these libraries, when you import from a specific file like /Button, they will almost always use a default export instead.

The solution would be to simply get the default export by doing:

import Button from 'react-bootstrap/Button'

With a default export, you can name the import anything you want, even something like this:

import MyButton from 'react-bootstrap/Button'

Alternatively, you can just import from the head folder. In that case, you would actually have to use {} to fetch the named items.

import {Button, Input, Form} from 'react-bootstrap';
Sign up to request clarification or add additional context in comments.

8 Comments

An awesome and simple explanation of named and default exports. A must learn for a person just starting out with React and Node.
tried everything you mentioned, but no hope. Now it is throwing from line no. 4 of Themeprovider.js. :(
@Joy are you using create-react-app? It sounds like you don't have your babel configured.
@Jezpoz thank you! I'm glad you think its helpful :)
that did it ! I hadn't use create-react-app when I created the project. Rather, I downloaded some project from Internet and kept modifying it. After I created a fresh project, everything is working fine ! Thanks a lot mate :) you guys are awesome !
|
2

try this:

import {Button} from 'react-bootstrap';

or:

 import Button from 'react-bootstrap/Button'

and you need to install bootstrap css and import it:

1.

npm install bootstrap

2.

import "bootstrap/dist/css/bootstrap.min.css"

3 Comments

Error is: TypeError: Object doesn't support property or method 'createContext' ./node_modules/react-bootstrap/es/ThemeProvider.js
@Joy i think this error come from your react and react-dom version try to upgrade them and try again! use npm update react react-dom
It's solved after I created a new project with create-react-app. Thanks a ton for the help mate !!
0

When you are importing a Button or any component wrapped around with curly braces, you do not need to include the component name after the backslash.

simply put

import {Button} from 'react-bootstrap/';

when we import a component without wrapped around with {}, we need to give the component name like this.

import Button from 'react-bootstrap/Button';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.