简体   繁体   English

Bootstrap手风琴无法正常反应

[英]Bootstrap accordion not working properly with react

Am very new to react and bootstrap. 是一个非常新的反应和引导程序。 I have run into a problem where the accordion is not working properly. 我遇到了手风琴无法正常工作的问题。 It has no color and when I click it slides quickly then disappears. 它没有颜色,当我单击它时,它会迅速滑动然后消失。 I have looked at getting started but it has not helped. 我已经看过入门,但并没有帮助。 What am I missing. 我想念什么。

Here is my code, thanks in advance 这是我的代码,谢谢

import React, { Component } from 'react';
import './App.css'
import Accordion from 'react-bootstrap/lib/Accordion'
import Panel from 'react-bootstrap/lib/Panel'
import Button from 'react-bootstrap/lib/Button'
import ButtonToolbar from 'react-bootstrap/lib/ButtonToolbar'
import Modal from 'react-bootstrap/lib/Modal'
import FormGroup from 'react-bootstrap/lib/FormGroup'
import ControlLabel from 'react-bootstrap/lib/ControlLabel'
import FormControl from 'react-bootstrap/lib/FormControl'

class App extends Component {

state = {
    recipes: [
    {recipeName: 'recipe1', Ingredients: ['cheese', 'cheese', 'cheese']},
    {recipeName: 'recipe2', Ingredients: ['cheese', 'cheese', 'cheese']},
    {recipeName: 'recipe3', Ingredients: ['cheese', 'cheese', 'cheese']}
    ]
}

  render() {
    const {recipes} = this.state;
    return (
      <div className="App container">
    <Accordion>
        {recipes.map((recipe, index)=>(
            <Panel header={recipe.recipeName} eventKey={index} key={index}>
                <ol>
                    {recipe.ingredients.map((item)=>(
                        <li key={item}>{item}</li>
                    ))}
                </ol>
            </Panel >
        ))}
    </Accordion>
      </div>
    );
  }
}
export default App;

I will answer my question. 我会回答我的问题。 Even though I had referred to getting started On the clipboard as I pasted, I still had bootstrap 4 CDN . 即使我在粘贴时提到了“ 入门” ,我仍然拥有bootstrap 4 CDN Yet through npm I had installed an older version of bootstrap 3. This was a conflict though some features like buttons could still work. 但是通过npm我安装了旧版的bootstrap3。这是一个冲突,尽管某些功能(如按钮)仍然可以使用。 The right stylesheet was this 正确的样式表是这个

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">

The Accordion code looks fine. 手风琴代码看起来不错。 Did you make sure to add the browser globals to the index.html. 您是否确定将浏览器全局变量添加到index.html。 I am pretty sure the Accordion relies on their javascript file. 我很确定手风琴依赖于他们的javascript文件。 Two things I did notice however. 但是我确实注意到两件事。

  1. Your state = needs to be inside a constructor after super(); 您的state =必须在super();之后放入构造函数中super(); and it should be this.state= . 它应该是this.state=

  2. You don't have to import every component from React Bootstrap individual library files like that. 您不必像这样从React Bootstrap单个库文件导入每个组件。 You could instead do 你可以做

    import { Accordion, Panel, Button, ButtonToolbar, Modal, FormGroup, ControlLabel, FormControl} from 'react-bootstrap';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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