What Is Difference Between Reactstrap and React-Bootstrap?
I Found Two Different Bootstraps for Reactjs Npm Install --Save Reactstrap React React-Dom Npm Install React-Bootstrap Bootstrap What Is the Basic and Main...
I found two different bootstraps for reactjs
- npm install --save reactstrap react react-dom
- npm install react-bootstrap bootstrap
What is the basic and main difference between both?
5 Answers
I have been struggling with this myself and it is as @Besart Marku says, highly opinion based.
One thing that did make a diffirence for me is that reactstraps documentation uses state in alot of its code examples:
import React from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
class ModalExample extends React.Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState(prevState => ({
modal: !prevState.modal
}));
}
render() {
return (
<div>
<Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
<Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
<ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={this.toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
export default ModalExample;
vs react-bootstrap uses functions and Hooks:
function MyVerticallyCenteredModal(props) {
return (
<Modal
{...props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
centered
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
Modal heading
</Modal.Title>
</Modal.Header>
<Modal.Body>
<h4>Centered Modal</h4>
<p>
Cras mattis consectetur purus sit amet fermentum. Cras justo odio,
dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac
consectetur ac, vestibulum at eros.
</p>
</Modal.Body>
<Modal.Footer>
<Button onClick={props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
function App() {
const [modalShow, setModalShow] = React.useState(false);
return (
<ButtonToolbar>
<Button variant="primary" onClick={() => setModalShow(true)}>
Launch vertically centered modal
</Button>
<MyVerticallyCenteredModal
show={modalShow}
onHide={() => setModalShow(false)}
/>
</ButtonToolbar>
);
}
render(<App />);
Im not giving an answer saying one of these is better than the other,its a preference and for me personaly I vent with reactstrap since I tend to use classcomponents more than Hooks so having finished examples that I can tweak with minimal effort did the trick.
They are 2 different libraries but both based on Bootstrap components.
Some little statistics about them
Thought I'd add my 2 pence. I came the other way, learning React Native with Android development before moving over to React.
The former method with Reactstrap is closer to how we have done things with Web Development and Bootstrap where as the latter is closer to how I have done things in React Native i.e. component based development.
Its really down to use case both make sense however if you have a dynamic page with lots of moving parts I would suggest using the React-Bootstrap library as the implementation is much closer to the component model and will allow you to make your page elements reusable (not that you can't do that with the former Reactstrap library).
I have gone with Reactstrap for now simply because it offers all of Bootstrap 4 out of the box which is what I need with very little fuss.
Official documentations:
Both work in the same way, from the perspective of use:
- They need
npm install bootstrapto import Bootstrap stylesheet file inside index.js or App.js to enable Bootstrap default styling withimport 'bootstrap/dist/css/bootstrap.min.css';. ReactJS, by default, will append all Bootstrap CSS code in a style tag inside head of HTML page.
import { StrictMode } from "react";
import ReactDOM from "react-dom";
import "bootstrap/dist/css/bootstrap.min.css";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
rootElement
);
- Give us ready-to-use Bootstrap Components redesigned internally as JSX, without the need to use JQuery or Javascript with direct DOM manipulation (using Virtual DOM, as React already works by default);
- Behind the scenes, using
React.createElementto render the components;
Props
The props passed to the components can have different names depending on the package. See the Button color usage:
import React from "react";
import { Button as ReactBootstrapButton } from "react-bootstrap";
import { Button as ReactstrapButton } from "reactstrap";
const App = () => (
<>
<ReactBootstrapButton variant="danger">React BootStrap</ReactBootstrapButton>
<ReactstrapButton color="danger">Reactstrap</ReactstrapButton>
</>
);
export default App;
The props names are different color and variant, but rendered HTML is pretty much the same, as we can see in DevTools:
Behind the scenes
You can view both implementations, comparing a basic component as Button in the packages source code:
- node_modules\react-bootstrap\cjs\Button.js (React Bootstrap ^1.6.0);
- node_modules\reactstrap\dist\reactstrap.cjs.js line:930 (Reactstrap v^8.9.0);
React Bootstrap
var Button = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
var bsPrefix = _ref.bsPrefix,
variant = _ref.variant,
size = _ref.size,
active = _ref.active,
className = _ref.className,
block = _ref.block,
type = _ref.type,
as = _ref.as,
props = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["bsPrefix", "variant", "size", "active", "className", "block", "type", "as"]);
var prefix = (0, _ThemeProvider.useBootstrapPrefix)(bsPrefix, 'btn');
var classes = (0, _classnames.default)(className, prefix, active && 'active', variant && prefix + "-" + variant, block && prefix + "-block", size && prefix + "-" + size);
if (props.href) {
return /*#__PURE__*/_react.default.createElement(_SafeAnchor.default, (0, _extends2.default)({}, props, {
as: as,
ref: ref,
className: (0, _classnames.default)(classes, props.disabled && 'disabled')
}));
}
if (ref) {
props.ref = ref;
}
if (type) {
props.type = type;
} else if (!as) {
props.type = 'button';
}
var Component = as || 'button';
return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, props, {
className: classes
}));
});
Button.displayName = 'Button';
Button.defaultProps = defaultProps;
var _default = Button;
exports.default = _default;
module.exports = exports["default"];