首页 > 解决方案 > 在 Netlify 上部署的 Gatsby Starter 上安装“react-bootstrap”

问题描述

我对盖茨比和反应都很陌生。我正在制作一个联系表单,并希望根据一个使用 react-bootstrap 组件的教程来完成它,但是我不知道如何将它安装在我通过我的 github 存储库部署的 gatsby 启动器上。

我的代码给出一个想法:

import React from 'react';

import Layout from '@common/Layout';
import Navbar from '@common/Navbar';

import Header from '@sections/Header';
import About from '@sections/About'; 
import Footer from '@sections/Footer';

import {Row, Col, Container, Form, Button} from "react-bootstrap" 

const IndexPage = () => (
  <Layout>
    <Navbar />
    <Header />
    <About>
      <Container>
        <Form name= "contact v1" method="post" data-netlify="true" onSubmit="submit">
         <input type="hidden" name="form-name" value="contact v1" />
          <Row>
            <Col>
              <Form.Group>
                <Form.Label>First Name </Form.Label>
                <Form.Control required size="lg" type="text"/>
              </Form.Group>
            </Col>
            <Col>
              <Form.Group>
                <Form.Label>Last Name </Form.Label>
                <Form.Control required size="lg" type="text"/>
              </Form.Group>
            </Col>
          </Row>
          <Form.Group>
                <Form.Label>How can we help? </Form.Label>
                <Form.Control required as="textarea" rows="3" placeholder="What do you do?"/>
          </Form.Group>
          <Button type="submit"> Submit </Button>
        </Form>
      </Container>
    </About>
                
    <Footer />
  </Layout>
);

export default IndexPage; 

这是我在 Netlify 上部署时的错误

3:08:08 PM: failed Building production JavaScript and CSS bundles - 15.912s
3:08:08 PM: error Generating JavaScript bundles failed
3:08:08 PM: Can't resolve 'react-bootstrap' in '/opt/build/repo/src/pages'
3:08:08 PM: If you're trying to use a package make sure that 'react-bootstrap' is installed. If you're trying to use a local file make sure that the path is correct.
3:08:08 PM: not finished Generating image thumbnails - 20.910s
3:08:08 PM: error Command failed with exit code 1.
3:08:08 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

标签: reactjsgatsbyyarnpkgcontact-formnetlify

解决方案


正如您在文档中看到的,只需运行:

npm install react-bootstrap bootstrap

请记住,当您这样做时:

import {Row, Col, Container, Form, Button} from "react-bootstrap" 

您正在从node_modules目录中导入组件,特别是从react-bootstrap文件夹中,因此您需要先安装依赖项。


推荐阅读