reactjs - react-bootstrap modülünün dışa aktarılmış üyeleri yok


Layout bileşenimde npm modülü react-bootstrap (https://www.npmjs.com/package/@types/react-bootstrap) kullanmak istiyorum (ASP.NET Core 2.1 şablon istemci projesinde de olduğu gibi). Ancak şablondaki dosya uzantısı .js. Typescript kullanmak istiyorum, bu yüzden Layout.tsx şöyle görünüyor:

import * as React from 'react';
import { Col, Grid, Row } from 'react-bootstrap/';           <----- compile time error: Module has no exported member col / grid / row
import NavMenu from './NavMenu';

interface Layout {
    children: any;
}

const Layout = ({ children }) => {
    return (<Grid fluid>
        <Row>
            <Col sm={3}>
                <NavMenu/>
            </Col>
            <Col sm={9}>
                {this.props.children}
            </Col>
        </Row>
    </Grid>);
}
export default Layout;

Benim package.json dosyam şöyle görünüyor:

{
  "name": "MyApp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@stitches/react": "^1.2.5",
    "@types/react-bootstrap": "^0.32.28",
    "@types/react-dom": "^17.0.11",
    "@types/react-router": "^5.1.17",
    "@types/react-router-bootstrap": "^0.24.5",
    "bootstrap": "^3.4.1",
    "react": "^17.0.2",
    "react-beautiful-dnd": "^13.1.0",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^6.0.0",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "rimraf": "^2.6.2"
  },
  "scripts": {
    "start": "rimraf ./build && react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "@types/react": "^17.0.34",
    "css-loader": "^6.5.1",
    "style-loader": "^3.3.1"
  }
}

Ayrıca index.tsx dosyasında 'bootstrap/dist/css/bootstrap.css' ve 'bootstrap/dist/css/bootstrap-theme.css' dosyalarını da ithal ettim.

Daha fazla bilgi:

  • ECMAScript sürümü: ECMAScript 2017
  • TSX dosyalarında JSX derlemesi: React
  • Modül Sistemi: ES2015

Sorunun nerede olabileceğini bilen var mı? Ben React'ta yeniyim, bu yüzden desteğiniz için teşekkürler!

Cevap #1

react-bootstrap'un Grid bileşeni yoktur, bunun yerine Container bileşeni vardır. Bunun dışında, bir fonksiyonel bileşende this.props.children kullanmamalısınız. Yani, kodunuz şöyle olmalıdır:

import React from 'react';
import Col from "react-bootstrap/Col";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import NavMenu from './NavMenu';

interface Layout {
    children: any;
}

const Layout = ({ children }) => {
    return (<Container fluid>
        <Row>
            <Col sm={3}>
                <NavMenu/>
            </Col>
            <Col sm={9}>
                {children}
            </Col>
        </Row>
    </Container>);
}
export default Layout;

Canlı bir çalışan örnek için bu kum havuzuna göz atabilirsiniz.