Adding and Editing Reviews
Learn how to implement the functionality that allows the addition and editing of reviews in a MERN stack application.
We'll cover the following...
In this lesson, we’ll learn how to implement the functionality of adding and editing reviews for our movie application.
import React from 'react' import { Switch, Route, Link } from "react-router-dom" import "bootstrap/dist/css/bootstrap.min.css" import AddReview from "./components/add-review" import MoviesList from "./components/movies-list" import Movie from "./components/movie" import Login from "./components/login" import Nav from 'react-bootstrap/Nav' import Navbar from 'react-bootstrap/Navbar' function App() { const [user, setUser] = React.useState(null) async function login(user = null){// default user to null setUser(user) } async function logout(){ setUser(null) } return ( <div className="App"> <Navbar bg="light" expand="lg"> <Navbar.Brand>Movie Reviews</Navbar.Brand> <Navbar.Toggle aria-controls="basic-navbar-nav" /> <Navbar.Collapse id="basic-navbar-nav"> <Nav className="mr-auto"> <Nav.Link> <Link to={"/movies"}>Movies</Link> </Nav.Link> <Nav.Link> { user ? ( <a onClick={logout}>Logout User</a> ) : ( <Link to={"/login"}>Login</Link> )} </Nav.Link> </Nav> </Navbar.Collapse> </Navbar> <Switch> <Route exact path={["/", "/movies"]} component={MoviesList}> </Route> <Route path="/movies/:id/review" render={(props)=> <AddReview {...props} user={user} /> }> </Route> <Route path="/movies/:id/" render={(props)=> <Movie {...props} user={user} /> }> </Route> <Route path="/login" render={(props)=> <Login {...props} login={login} /> }> </Route> </Switch> </div> ); } export default App;
When a logged-in user goes to a specific movie’s page and clicks
“Add Review,” we’ll render the AddReview
component for the user to submit a review, as shown below:
We’ll also use the AddReview
component to edit a review. That is, when a user clicks the “Edit” link in an existing review, as shown in the image below:
When editing, we’ll render the AddReview
component but with the header “Edit Review,” as shown below. The existing review text will be shown, and users can then edit and submit.
The AddReview
component will allow us to ...