4

Earlier my app was in ReactJs + React-bootstrap. Now I'm using Typescript + ReactJs + React-bootstrap

To reduce the size of the app for production Earlier I used to import react-bootstrap components using - import Button from 'react-bootstrap/lib/Button'

After adding Typescript it shows the following error

ERROR in [at-loader] ./components/Hello.tsx:6:8 TS1192: Module ''react-bootstrap/lib/Button'' has no default export.

Trial 1

import {Button} from 'react-bootstrap/lib/Button' but in this case Button is undefined.

Trial 2

import * as Button from 'react-bootstrap/lib/Button' but in this case another error pops out

ERROR in [at-loader] ./components/Hello.tsx:54:12 TS2604: JSX element type 'Button' does not have any construct or call signatures.

This error shows error in line <Button onClick={handleClick} bsStyle="danger" bsClass="glyphicon glyphicon-new-window"></Button>

Though import {Button} from 'react-bootstrap' works fine but this is not desired because it results in increasing the size of the app by 200kbs for me.

How can we import specific component from react-bootstrap using Typescript??

7
  • 1
    @ShubhamKhatri I already use uglify js. But there is an increase in size when we use import {Button} from 'react-bootstrap' as whole react-bootstrap gets imported but in {Button} the Button component gets imported. Its tried and tested that there is increase in size Commented Apr 11, 2017 at 6:33
  • Hm, I think, because of the way react-bootstrap is bundled, it is not possible for UglifyJS to only import everything necessary. require calls kill all possible optimization. If you want to keep your app size small the way @iamsaksham is doing it, is the correct way. Commented Apr 11, 2017 at 6:39
  • @SebastianSebald I haven't tested it but I think {Button} should import only the button component and not everything that what {} are for. If you compare a similar approach is done with destructuring props Commented Apr 11, 2017 at 6:42
  • @ShubhamKhatri lets not deviate from the issue, importing react-bootstrap for sure increases size but react-bootstrap/lib/Button is quite cheap in size. Commented Apr 11, 2017 at 6:46
  • Yes it only imports the Button, but it will include the whole library in the bundle anyway :( This does not happen if you import from lib. Commented Apr 11, 2017 at 6:46

1 Answer 1

1

Since TypeScript adheres to the ESModule spec but the lib is using CommonJS, you have to to the sam, you did with the React import when you switched to TypeScript.

JS

import React from 'react'
import Button from 'react-bootstrap/lib/Button'

TypeScript

import * as React from 'react'
import * as Button from 'react-bootstrap/lib/Button'

Shameless self plug: I wrote about the reason for this here. I guess you will run into this issue in the future again :)

This is the version i used:

{
  "@types/react-bootstrap": "^0.0.47",
  "react-bootstrap": "^0.30.8"
}
Sign up to request clarification or add additional context in comments.

11 Comments

I already tried using import * as Button from 'react-bootstrap/lib/Button'. In this case there is another compilation error ` ERROR in [at-loader] ./components/Hello.tsx:54:12` TS2604: JSX element type 'Button' does not have any construct or call signatures.
Hmm. I used this to test it. Everything works "on my machine". Maybe it is an issue with the awesome-typescript-loader? The linked CLI uses the ts-loader.
From where did you use the typings for react-bootstrap? Because it says the same error for me with ts-loader as well
I added the dependencies part of bootstrap. Note that I do not have any types for Button if I am importing it this way :( The app works though. Could be possible that the typings are not 100% correct.
Man!!!! This typescript with react-bootstrap is killing me, no hard feelings brother. Can u make a plnkr or jsfiddle?? I'll make for my issue as well
|

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.