如何正确添加引导主题Navbar到React

时间:2017-09-19 09:55:59

标签: javascript reactjs react-bootstrap

我有一个正在处理node.js + react + bootstrap的应用程序 这是工作。但是当我尝试在浏览器中使用它时,我无法启动它。 我尝试手动连接主题(从github下载所有这些主题,选择一个并连接) 不知怎的,它无法工作。所以我在节点上留下了之前的所有内容。

如何在我的应用程序中使用bootstrap中的导航栏等。 我有2个文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Example</title>
    <script src="react.js"></script>
    <script src="react-dom.js"></script>
    <script src="babel.min.js"></script>
    <link rel="stylesheet" type="text/css" href="bootstrap.css">
</head>
<body>
    <div id="root"></div>
    <script src="index.js" type="text/jsx"></script>
</body>
</html>

&#13;
&#13;
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import "bootswatch/readable/bootstrap.css";
import { Navbar, NavItem, Nav, Grid, Row, Col } from "react-bootstrap";

const PLACES = [
  { name: "Palo Alto", zip: "94303" },
  { name: "San Jose", zip: "91088" },
  { name: "Santa Cruz", zip: "95062" },
  { name: "Honolulu", zip: "96803" }
];

class WeatherDisplay extends Component {
  constructor() {
    super();
    this.state = { weatherData: null };
  }
  componentDidMount() {
    const zip = this.props.zip;
    const URL = "http://api.openweathermap.org/data/2.5/weather?q=" +
      zip +
      "&appid=b1b35bba8b434a28a0be2a3e1071ae5b&units=imperial";
    fetch(URL).then(res => res.json()).then(json => {
      this.setState({ weatherData: json });
    });
  }
  render() {
    const weatherData = this.state.weatherData;
    if (!weatherData) return <div>Loading</div>;
    const weather = weatherData.weather[0];
    const iconUrl = "http://openweathermap.org/img/w/" + weather.icon + ".png";
    return (
      <div>
        <h1>
          {weather.main} in {weatherData.name}
          <img src={iconUrl} alt={weatherData.description} />
        </h1>
        <p>Current: {weatherData.main.temp}°</p>
        <p>High: {weatherData.main.temp_max}°</p>
        <p>Low: {weatherData.main.temp_min}°</p>
        <p>Wind Speed: {weatherData.wind.speed} mi/hr</p>
      </div>
    );
  }
}

class App extends Component {
  constructor() {
    super();
    this.state = {
      activePlace: 0
    };
  }
  render() {
    const activePlace = this.state.activePlace;
    return (
	<div>
  <Navbar>
    <Navbar.Header>
      <Navbar.Brand>
        React Simple Weather App
      </Navbar.Brand>
    </Navbar.Header>
  </Navbar>
  <Grid>
    <Row>
      <Col md={4} sm={4}>
        <h3>Select a city</h3>
        <Nav bsStyle="pills" stacked activeKey={activePlace} onSelect={index => {this.setState({ activePlace: index }); }}>
          {PLACES.map((place, index) => ( <NavItem key={index} eventKey={index}>{place.name}</NavItem> ))}
        </Nav>
      </Col>
      <Col md={8} sm={8}>
        <WeatherDisplay key={activePlace} zip={PLACES[activePlace].zip} />
      </Col>
    </Row>
  </Grid>
	</div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
&#13;
&#13;
&#13;

我在我的计算机上的app文件夹中使用脱机反应和babel文件 最后,我想在浏览器中打开index.htm并查看带有bootstrap的React应用程序

0 个答案:

没有答案