简体   繁体   中英

Unable to use react-bootstrap in react typescript project even after adding @types/react-bootstrap to my project

I am unable to use the react-bootstrap components in my react typescript project even after adding "@types/react-bootstrap".

React Bootstrap is listed in the package.json file.

This is my SideBar component.

import React from "react";
import { Navbar } from "react-bootstrap";

const SideBar: React.FC = () => {
  return (
    <>
      <Navbar bg="dark" variant="dark">
        <Navbar.Brand href="#home">
          <img alt="logo" src="./logo.png" width="30" height="30" className="d-inline-block align-top" /> React Bootstrap
        </Navbar.Brand>
      </Navbar>
    </>
  );
};

export default SideBar;

This is index.tsx file

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();

This is my error

Failed to compile.

/Users/kishore/Documents/truexam-admin/node_modules/react-bootstrap/esm/index.d.ts
TypeScript error in /Users/kishore/Documents/truexam-admin/node_modules/react-bootstrap/esm/index.d.ts(2,1):
Declaration or statement expected.  TS1128

    1 | export { default as Accordion } from './Accordion';
  > 2 | export type { AccordionProps } from './Accordion';
      | ^
    3 | export { default as AccordionContext } from './AccordionContext';
    4 | export { default as AccordionCollapse } from './AccordionCollapse';
    5 | export type { AccordionCollapseProps } from './AccordionCollapse';

There are two ways you can fix it: either upgrade typescript to 3.8+ or downgrade your react-bootstrap to 1.0.1 as listed in this answer https://stackoverflow.com/a/62942393/5972355 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM