使用Reactjs和Bootstraps一次显示一个折叠内容。
下面的代码使用jquery和bootstrap4来显示和隐藏折叠内容,当每个按钮一次单击一个并运行良好时。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script>
$( document ).ready(function() {
$('.collapse').on('show.bs.collapse', function () {
// hide all accordion except the clicked one
$('.collapse').not(this).collapse('hide');
});
});
</script>
</head>
<body>
<a href="#content1" data-toggle="collapse">Content1..</a>
<a href="#content2" data-toggle="collapse">Content2</a>
<a href="#content3" data-toggle="collapse">Content3</a>
<br>
<div id="content1" class="collapse ">
<h1>Content 1 records</h1>
<a href="#content1" class="btn btn-default" data-toggle="collapse">Close </a>
</div>
<div id="content2" class="collapse ">
<h1>Content 2 records</h1>
<a href="#content2" class="btn btn-default" data-toggle="collapse">Close</a>
</div>
<div id="content3" class="collapse ">
<h1>Content 3 records</h1>
<a href="#content3" class="btn btn-default" data-toggle="collapse">Close</a>
</div>
</body></html>
现在我必须在反应式中得到它的等价物。我已经安装了引导程序和Reactjs代码,每次单击按钮时都会显示折叠内容。
这里是我对Reactjs.的问题,我希望它只显示为一个折叠的内容,就像上面的jquery中的一样。请如何将此jquery代码转换为reactjs等效项,或者是否有其他选项?
$( document ).ready(function() {
$('.collapse').on('show.bs.collapse', function () {
// hide all accordion except the clicked one
$('.collapse').not(this).collapse('hide');
});
});
这里是Reactjs代码
import React, { Component, Fragment } from "react";
import { render } from "react-dom";
import $ from 'jquery';
class Collapse extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
return (
<span>
<label>
<ul>
<a href="#content1" data-toggle="collapse">Content1..</a>
<a href="#content2" data-toggle="collapse">Content2</a>
<a href="#content3" data-toggle="collapse">Content3</a>
<br />
<div id="content1" class="collapse ">
<h1>Content 1 records</h1>
<a href="#content1" class="btn btn-default" data-toggle="collapse">Close </a>
</div>
<div id="content2" class="collapse ">
<h1>Content 2 records</h1>
<a href="#content2" class="btn btn-default" data-toggle="collapse">Close</a>
</div>
<div id="content3" class="collapse ">
<h1>Content 3 records</h1>
<a href="#content3" class="btn btn-default" data-toggle="collapse">Close</a>
</div>
</ul>
</label>
</span>
);
}
}
发布于 2019-01-28 14:30:21
下面是一个示例代码,展示了如何做到这一点:
class App extends React.Component {
constructor(props) {
super(props);
this.toggleAccordion = this.toggleAccordion.bind(this);
this.state = {
accordion: [true, false, false],
};
}
toggleAccordion(tab) {
const prevState = this.state.accordion;
const state = prevState.map((x, index) => tab === index ? !x : false);
this.setState({
accordion: state,
});
}
render() {
return (
<div className='wrapper'>
<div id="accordion">
<Reactstrap.Card className="mb-0">
<Reactstrap.CardHeader id="headingOne">
<Reactstrap.Button block color="link" className="text-left m-0 p-0" onClick={() => this.toggleAccordion(0)} aria-expanded={this.state.accordion[0]} aria-controls="collapseOne">
<h5 className="m-0 p-0">Collapsible Group Item #1</h5>
</Reactstrap.Button>
</Reactstrap.CardHeader>
<Reactstrap.Collapse isOpen={this.state.accordion[0]} data-parent="#accordion" id="collapseOne" aria-labelledby="headingOne">
<Reactstrap.CardBody>
1. Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non
cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft
beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</Reactstrap.CardBody>
</Reactstrap.Collapse>
</Reactstrap.Card>
<Reactstrap.Card className="mb-0">
<Reactstrap.CardHeader id="headingTwo">
<Reactstrap.Button block color="link" className="text-left m-0 p-0" onClick={() => this.toggleAccordion(1)} aria-expanded={this.state.accordion[1]} aria-controls="collapseTwo">
<h5 className="m-0 p-0">Collapsible Group Item #2</h5>
</Reactstrap.Button>
</Reactstrap.CardHeader>
<Reactstrap.Collapse isOpen={this.state.accordion[1]} data-parent="#accordion" id="collapseTwo">
<Reactstrap.CardBody>
2. Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non
cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft
beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</Reactstrap.CardBody>
</Reactstrap.Collapse>
</Reactstrap.Card>
<Reactstrap.Card className="mb-0">
<Reactstrap.CardHeader id="headingThree">
<Reactstrap.Button block color="link" className="text-left m-0 p-0" onClick={() => this.toggleAccordion(2)} aria-expanded={this.state.accordion[2]} aria-controls="collapseThree">
<h5 className="m-0 p-0">Collapsible Group Item #3</h5>
</Reactstrap.Button>
</Reactstrap.CardHeader>
<Reactstrap.Collapse isOpen={this.state.accordion[2]} data-parent="#accordion" id="collapseThree">
<Reactstrap.CardBody>
3. Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non
cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft
beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</Reactstrap.CardBody>
</Reactstrap.Collapse>
</Reactstrap.Card>
</div>
</div>
);
}
}
ReactDOM.render( < App / > ,
document.getElementById('root')
);
.wrapper {
margin: 50px;
}
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reactstrap/6.0.1/reactstrap.full.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" crossorigin="anonymous">
<div id="root" />
https://stackoverflow.com/questions/54404814
复制相似问题