Extract a component
The shop's card markup has grown: a cover, a title, a price, a badge and two kinds of button. Time to give it a file of its own.
A component is a .pzl file like a view, used as a tag. Any tag that starts with a capital letter is a component, so after import AlbumCard from '../components/AlbumCard.pzl', the view can write <AlbumCard album={ album }/>. Its attributes arrive as props: the second argument of data(params, props).
data(params, props) {
return { album: props.album };
}
A component's <puzzle-view> wraps a single root element, here the card's <li>. Classes and other attributes go on that root, never on <puzzle-view> itself.
components/AlbumCard.pzl is what puzzle generate component AlbumCard writes: a stub button with a label prop. You will replace all of it.
Your turn
- In
AlbumCard.pzl, replace the stub's markup with the whole<li class="card">…</li>fromShop.pzl, and returnalbum: props.albumfromdata(). Delete the stub's<style>and itshandleClick. - Give the card a stand-in
addhandler, so its buttons have something to call:add: () => console.log('Add', this.props.album.title). - In
Shop.pzl, importAlbumCardand replace the<li>inside the loop with<AlbumCard album={ album }/>.
The shop looks exactly as before. Click Add and the console logs the album: the button now belongs to the card, while the playlist still belongs to the shop. The next lesson connects the two.
A list of components is keyed like a list of elements. Albums are store records, so each card keys itself by the album's id.