React — Bootstrap
Installing Bootstrap
npm install react-bootstrap bootstrap
Basic Usage
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button, Container, Row, Col } from 'react-bootstrap';
function App() {
return (
<Container>
<Row>
<Col>
<Button variant="primary">Click me</Button>
</Col>
</Row>
</Container>
);
}
Components
import {
Navbar,
Nav,
Card,
Form,
Table,
Alert,
Modal
} from 'react-bootstrap';
function App() {
return (
<div>
<Navbar bg="dark" variant="dark">
<Navbar.Brand href="#home">My App</Navbar.Brand>
<Nav className="mr-auto">
<Nav.Link href="#home">Home</Nav.Link>
<Nav.Link href="#features">Features</Nav.Link>
</Nav>
</Navbar>
<Container className="mt-4">
<Card>
<Card.Header>Card Title</Card.Header>
<Card.Body>
<Card.Text>Card content</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</Container>
</div>
);
}
Bootstrap Examples
// Form with validation
function ContactForm() {
const [validated, setValidated] = useState(false);
const handleSubmit = (e) => {
e.preventDefault();
const form = e.currentTarget;
if (form.checkValidity() === false) {
e.stopPropagation();
}
setValidated(true);
};
return (
<Form noValidate validated={validated} onSubmit={handleSubmit}>
<Form.Group>
<Form.Label>Name</Form.Label>
<Form.Control required type="text" placeholder="Name" />
<Form.Control.Feedback>Looks good!</Form.Control.Feedback>
</Form.Group>
<Form.Group>
<Form.Label>Email</Form.Label>
<Form.Control required type="email" placeholder="Email" />
<Form.Control.Feedback type="invalid">
Please enter a valid email.
</Form.Control.Feedback>
</Form.Group>
<Button type="submit">Submit</Button>
</Form>
);
}
// Responsive grid
function ProductGrid() {
return (
<Container>
<Row>
<Col xs={12} md={6} lg={4}>
<Card>
<Card.Img variant="top" src="product1.jpg" />
<Card.Body>
<Card.Title>Product 1</Card.Title>
<Button variant="primary">Buy</Button>
</Card.Body>
</Card>
</Col>
<Col xs={12} md={6} lg={4}>
<Card>
<Card.Img variant="top" src="product2.jpg" />
<Card.Body>
<Card.Title>Product 2</Card.Title>
<Button variant="primary">Buy</Button>
</Card.Body>
</Card>
</Col>
</Row>
</Container>
);
}
Mini Practice
Write React code that:
- Installs and imports Bootstrap
- Uses Bootstrap components
- Creates a responsive layout
- Implements form validation
Up Next
Next: Learn about Tailwind CSS in React.
Related Topics
Frequently Asked Questions about Bootstrap
What is Bootstrap in React?
Bootstrap is a fundamental concept in React. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Bootstrap?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Bootstrap.
Why is Bootstrap important in React?
Bootstrap is essential for React development. Understanding this concept will help you write better code and solve real-world problems more effectively.