19 - React Bootstrap and State - Exercise
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. If you don’t have AllDepartments and Department components, please download them from here
Task 1 - Include Bootstrap
Open a terminal (either Terminal in VSCode or Windows Powershell) pointing to your project folder. For this task we will work with the
Department
component from the previous exercise.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.
Task 2 - Adding State
Create a new component called DepartmentForm.jsx
We will use Bootstrap Form component to help us create a nice looking form. We only need two fields - department name and team id.
return ( <Form> <Form.Group className="mb-3" controlId="formDeptName"> <Form.Label>Department Name</Form.Label> <Form.Control type="text" placeholder="Enter department name"/> </Form.Group> <Form.Group className="mb-3" controlId="formTeamId"> <Form.Label>Team Id</Form.Label> <Form.Control type="text" placeholder="Enter team id"/> </Form.Group> <Button variant="primary" type="submit"> Submit </Button> </Form> )
In this case, we will need two variables - deptName and teamId - that will constitute the state for this component. They will change based on what the user types. So we need to declare them as state.
The input fields in the form we just created will be controlled components. We will pass in the initial values and make sure we get the updated values into our state variables whenever the user makes a change.
The state variables are now updated correctly with the values from the input field. The last thing to handle is the submit button. Let’s add an event handler for
onSubmit
event into the<Form>
element.Create an anonymous function and assign it to a variable called
addDepartment
. The function will accept one parameter -e
- that represents the object of the event. First thing we will need to do it to stop the default behaviour - prevent the browser to create a post request. To do so, use the event'spreventDefault()
method.Within the body of the anonymous functions, create a JSON object to represent the department - assign it an
id
,dep_name
would have the value of thedeptName
state andteam_id
would have the value ofteamID
state variable.You can use
console.log
to see the object's values.Add the
DepartmentForm
to the view - include it into theAllDepartments.jsx
component. We can use the Bootstrap layout components to help us to place our form next to the Cards showing the departments information.
Task 3 - Add inverse flow
We would like to add the previously created JSON dept object to our list of departments that resides in the
AllDepartments.jsx
component. That is also the parent of theDepartmentForm.jsx
.We will create the inverse flow - pass a handler from the parent(
AllDepartments
) to the child (DepartmentForm
) component.The
addDepartment
is a variable holding an anonymous function that adds a new element to the array of departments.In the child component -
DepartmentForm
- use the submitHandler props to invoke theaddDepartment
function.When you run it, open your Inspect tool in the browser and you should see the updated array in your Console.