Overview
In this exercise we will practice including Bootstrap library to our project. We will create state for our components and swap rendered components using router functionality.
Task 1 - Include Bootstrap
Open a terminal (either Terminal in VSCode or Windows Powershell) pointing to your project folder. For this task use the project from the last exercise. If you don’t have AllDepartments and Department components, please download them from here
Type in
npm install react-bootstrap bootstrap
. After a short installation you should see the process finished with no errors. You may see some audit warnings, we will disregard those for now.Open your package.json and check that the Bootstrap dependency has been indeed added.
In index.js include the following bootstrap css link
import 'bootstrap/dist/css/bootstrap.min.css';
Create a new folder inside your src folder called
assets
. This is where all the pictures will go that we will use in our website. Download 5 random pictures from the internet, rename them to1.jpg
,2.jpg
,3.jpg
,4.jpg
and5.jpg
and place them into this folder.We will use a Bootstrap component to help us style our Department component. We will use a Bootstrap Card component. https://react-bootstrap.github.io/components/cards/
Inside the Department component, replace the previous code with the Card component code. We will use the information from the props to be displayed in the Card and also dynamically generate the picture source.
<Card style={{ width: '18rem' }}> <Card.Img variant="top" src={require(`../assets/${props.dept.dep_id}.jpg`)} /> <Card.Body> <Card.Title>{props.dept.dep_name}</Card.Title> <Card.Text> This is team {props.dept.team_id}. </Card.Text> <Button variant="primary">Go somewhere</Button> </Card.Body> </Card>
Run
npm start
and check the view in your browser.