Hello fellow learners!
I’ve been trying to complete the Jammming project but am having trouble passing the data stored in my searchresults array to the playlist array. I created an event handler to pass the searchresults array info to the playlist array when a track is clicked on in order to propogate that playlist. The way I organized my file structure the searchresults component and playlist component are siblings so I attempted to pass the data from the parent down to the children but upon doing so I get a syntheticbaseevent instead of a track with the correct info .
This is what the result looks like…
Does anyone know what the solution is to this?
Thanks for taking the time to review my code!
This is my Top level component “App”
import { useState, useCallback } from "react";
import SearchBar from "./SearchBar";
import SearchResults from "./SearchResults";
import Playlist from "./Playlist";
import styles from "../Styles/App.module.css";
function App() {
const [playlistTracks, setPlaylistTracks] = useState([
{
song: "Go Baby",
artist: "Lupe",
album: "Food and Liqour",
id: 1,
},
]);
const [searchResults, setSearchResults] = useState([
{
song: "Go Baby",
artist: "Lupe",
album: "Food and Liqour",
id: 1,
},
{
song: "Demon Days",
artist: "Gorillaz",
album: "Self titled",
id: 2,
},
]);
const onAdd = (track) => {
setPlaylistTracks((prevTracks) => [...prevTracks, track]);
track.cancelable = false;
console.log(`track is ${track}`);
};
return (
<div id={styles.app}>
<header>
<h1>Spotify Playlist App</h1>
</header>
<SearchBar />
<main>
<SearchResults searchResults={searchResults} onAdd={onAdd} />
<Playlist playlistTracks={playlistTracks} />
</main>
</div>
);
}
export default App;
Search Results
import React from "react";
import styles from "../Styles/SearchResults.module.css";
import Tracklist from "./Tracklist";
function SearchResults(props) {
return (
<div id={styles.searchResults}>
<h3>Results</h3>
<Tracklist tracks={props.searchResults} onAdd={props.onAdd} />
</div>
);
}
export default SearchResults;
Playlist
import React from "react";
import styles from "../Styles/Playlist.module.css";
import Tracklist from "./Tracklist";
function Playlist(props) {
const { playlistTracks, onRemove } = props;
console.log(playlistTracks);
return (
<div id={styles.playlist}>
<h3>Playlist</h3>
<input type="text" />
<Tracklist tracks={playlistTracks} />
<button>Save to Spotify</button>
</div>
);
}
export default Playlist;
Tracklist
import React from "react";
import Track from "./Track";
function Tracklist(props) {
return (
<div>
{props.tracks.map((track) => {
return <Track track={track} onAdd={props.onAdd} />;
})}
</div>
);
}
export default Tracklist;
Track
import React, { useCallback } from "react";
import styles from "../Styles/Track.module.css";
function Track(props) {
const choice = () => {
return <button onClick={props.onAdd}>+</button>;
};
return (
<div id={styles.track}>
<div id={styles.track_info}>
<h4>{props.track.song}</h4>
<p>
{props.track.artist} | {props.track.album}
</p>
</div>
{choice()}
</div>
);
}
export default Track;```