Full Stack Web App using Vue.js & Express.js - Part 4
Creating a Full-Stack App with Music Lyrics: Part 4
As we continue our journey through creating a full-stack app, we're now working on integrating music lyrics into our application. We've created a basic structure for our app, including a database to store song information and a frontend to display the data. In this part of the tutorial, we'll focus on adding a view button that allows users to navigate to individual songs.
To achieve this, we need to add some functionality to our frontend code. We'll start by modifying our existing code to include a "Let's See Song Title" section, which will display the title, artist, and genre of each song. To do this, we'll use the `song.title`, `song.artist`, and `song.genre` properties to access the data stored in our database.
On one side of the screen, we'll create a "Left Side" section that includes a view button. This button will navigate users to the individual song details page when clicked. To achieve this, we need to add some JavaScript code that binds the `song.id` property to a URL parameter when the button is clicked.
For example, our HTML code might look like this:
```html
{{ song.title }} by {{ song.artist }} ({{ song.genre }})
```
And our JavaScript code might look like this:
```javascript
class ViewButton extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.onClick = this.onClick.bind(this);
}
onClick(event) {
// navigate to individual song details page
navigateTo(`/song/${this.props.song.id}`);
}
render() {
return (
);
}
}
export default ViewButton;
```
In this code, we've created a `ViewButton` component that includes an `onClick` event handler. When the button is clicked, this handler navigates to the individual song details page using the `/song/${this.props.song.id}` URL parameter.
We'll also need to add a new route for navigating to individual songs. We can do this by adding a new file to our project's routing configuration. For example:
```javascript
import { Route } from 'react-router-dom';
const routes = [
{
path: '/',
component: Home,
},
{
path: '/song/:id',
component: SongDetails,
},
];
export default routes;
```
In this code, we've added a new route that listens for the `/song/:id` URL parameter. When this route is matched, it renders the `SongDetails` component.
Finally, we can implement the individual song details page by creating a new file called `SongDetails.js`. This file will display the title, artist, and genre of the selected song.
Here's an example implementation:
```javascript
import React from 'react';
import { navigate } from 'react-router-dom';
class SongDetails extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidMount() {
// fetch individual song data from database
const id = this.props.match.params.id;
fetch(`/song/${id}`)
.then(response => response.json())
.then(data => this.setState({ song: data }));
}
render() {
return (
{this.state.song.title}
By {this.state.song.artist} ({this.state.song.genre})
{/* display other song details here */}
);
}
}
export default SongDetails;
```
In this code, we've created a `SongDetails` component that fetches the individual song data from our database using the `/song/${id}` URL parameter. We then render the title, artist, and genre of the selected song.
That's it for Part 4 of our full-stack app tutorial! We've added a view button that navigates users to individual songs, as well as implemented the individual song details page. Stay tuned for the next part, where we'll continue building out our application.