【问题标题】:reactstrap navbar header seems to be missingreactstrap 导航栏标题似乎丢失了
【发布时间】:2018-05-22 16:45:01
【问题描述】:

我目前正在尝试使用 reactstrap 创建导航栏,我想使用

<div className="navbar-header">
</div>

这将映射到 React-Bootstrap 中的 Navbar.Header。

不幸的是,我在 reactstrap 中找不到这样的组件。我错过了什么吗?

【问题讨论】:

    标签: twitter-bootstrap reactjs react-bootstrap reactstrap


    【解决方案1】:

    尊敬的先生:

    React 并不能完全按照您尝试使用它的方式工作......好吧,如果您将 CDN 加载到 index.html 的标头中,它将使用它。如果您使用的是 reactstrap 或 react-bootstrap,则可以将它们作为自定义组件加载。

    要在 react-bootstrap 中加载按钮,您首先需要导入它:

    import {Button} from 'react-bootstrap'

    在您的代码中,您可以这样称呼它:

    &lt;Button bsStyle="warning" bsSize="large"&gt; Button! &lt;/Button&gt;

    这是调用您在普通 html 中会执行的操作的 react 方法,如下所示:

    &lt;button class="btn btn-lg btn-warning&gt; Button! &lt;/button&gt;

    您仍然需要从本地或 index.html 中的 CDN 加载 css(通常保存在 public/index.html 中)。

    https://react-bootstrap.github.io/components.html

    【讨论】:

      【解决方案2】:
      import React from 'react';
      import {
        Collapse,
        Navbar,
        NavbarToggler,
        NavbarBrand,
        Nav,
        NavItem,
        NavLink,
        UncontrolledDropdown,
        DropdownToggle,
        DropdownMenu,
        DropdownItem } from 'reactstrap';
      
      export default class Example extends React.Component {
        constructor(props) {
          super(props);
      
          this.toggle = this.toggle.bind(this);
          this.state = {
            isOpen: false
          };
        }
        toggle() {
          this.setState({
            isOpen: !this.state.isOpen
          });
        }
        render() {
          return (
            <div>
              <Navbar color="faded" light expand="md">
                <NavbarBrand href="/">reactstrap</NavbarBrand>
                <NavbarToggler onClick={this.toggle} />
                <Collapse isOpen={this.state.isOpen} navbar>
                  <Nav className="ml-auto" navbar>
                    <NavItem>
                      <NavLink href="/components/">Components</NavLink>
                    </NavItem>
                    <NavItem>
                      <NavLink href="https://github.com/reactstrap/reactstrap">Github</NavLink>
                    </NavItem>
                    <UncontrolledDropdown nav inNavbar>
                      <DropdownToggle nav caret>
                        Options
                      </DropdownToggle>
                      <DropdownMenu >
                        <DropdownItem>
                          Option 1
                        </DropdownItem>
                        <DropdownItem>
                          Option 2
                        </DropdownItem>
                        <DropdownItem divider />
                        <DropdownItem>
                          Reset
                        </DropdownItem>
                      </DropdownMenu>
                    </UncontrolledDropdown>
                  </Nav>
                </Collapse>
              </Navbar>
            </div>
          );
        }
      }
      <!--Navbar Properties-->
      Navbar.propTypes = {
        light: PropTypes.bool,
        dark: PropTypes.bool,
        fixed: PropTypes.string,
        color: PropTypes.string,
        role: PropTypes.string,
        expand: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
        tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
        // pass in custom element to use
      }
      <!--NavbarBrand Properties-->
      NavbarBrand.propTypes = {
        tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
        // pass in custom element to use
      }
      <!--NavbarToggler-->
      import React from 'react';
      import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';
      
      export default class Example extends React.Component {
        constructor(props) {
          super(props);
      
          this.toggleNavbar = this.toggleNavbar.bind(this);
          this.state = {
            collapsed: true
          };
        }
      
        toggleNavbar() {
          this.setState({
            collapsed: !this.state.collapsed
          });
        }
        render() {
          return (
            <div>
              <Navbar color="faded" light>
                <NavbarBrand href="/" className="mr-auto">reactstrap</NavbarBrand>
                <NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
                <Collapse isOpen={!this.state.collapsed} navbar>
                  <Nav navbar>
                    <NavItem>
                      <NavLink href="/components/">Components</NavLink>
                    </NavItem>
                    <NavItem>
                      <NavLink href="https://github.com/reactstrap/reactstrap">Github</NavLink>
                    </NavItem>
                  </Nav>
                </Collapse>
              </Navbar>
            </div>
          );
        }
      }
      <!--NavbarToggler Properties-->
      NavbarToggler.propTypes = {
        type: PropTypes.string,
        tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
        // pass in custom element to use
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-13
        • 1970-01-01
        • 2018-05-17
        • 2017-09-08
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        相关资源
        最近更新 更多