16
loading...
This website collects cookies to deliver better user experience
useState
, which is a hook provided by React that allows us to set information dynamically, and monitor it in a manner that will allow our components to change along with updates to that information.import React, { useState } from 'react';
function App () {
const [ playList, setPlayList ] = useState ([
{
id:1,
genre:"Alternative",
title:"You and I",
artist:"Toro y Moi",
track: "/tracks/you_and_i.mp3"
}, {
id:2,
genre:"Rock",
title:"Khuda Bhi Aasman",
artist:"Khruangbin",
track: "/tracks/khuda_bhi_aasman.mp3"
}, {
id:3,
genre:"Rock",
title:"Goodie Bag",
artist:"Still Woozy",
track: "/tracks/goodie_bag.mp3"
}
])
return(
<div className=music-box>
<MusicBox playlist={playlist} setPlaylist={setPlaylist} />
</div>
)}
export default App
MusicBox
will probably use the information to populate some interface.playlist
value. Also, it would be a safe bet that not every component would need every value inside of playlist
. We could still pass everything down manually via prop drilling
but that could get cumbersome if we are also passing other prop values declared within smaller subsections of the application.context
exists. Imagine if you only had to declare your state that a whole application might need pieces of in a single spot, and only call those values when and where you needed them, no need for prop drilling!Context
is the container, Provider
is what lets your application components reach into the container.import React,{ useState } from "react";
//Container
const PlaylistContext = React.createContext();
//Allows access to the container
function PlaylistProvider({ children }) {
const [ playList, setPlayList ] = useState ([
{
id:1,
genre:"Alternative",
title:"You and I",
artist:"Toro y Moi",
track: "/tracks/you_and_i.mp3"
}, {
id:2,
genre:"Rock",
title:"Khuda Bhi Aasman",
artist:"Khruangbin",
track: "/tracks/khuda_bhi_aasman.mp3"
}, {
id:3,
genre:"Rock",
title:"Goodie Bag",
artist:"Still Woozy",
track: "/tracks/goodie_bag.mp3"
}
])
return(
<PlaylistContext.Provider value={{ playList, setPlayList }}>
{children}
</PlaylistContext.Provider>;
)
}
export { PlaylistContext, PlaylistProvider };
Provider
.App
component and see what it looks like now.import React from 'react';
//This is our means of access
import { PlaylistProvider } from "./user";
function App () {
return(
<PlaylistProvider>
<div className=music-box>
<MusicBox />
</div>
</PlaylistProvider>
)}
export default App
MusicBox
or lower can now gain access to our playlist state. Up until now however, we have merely built the bridge from our context to the components that need to access them. We still need to cross the bridge and bring back the information.import { useContext } from 'react';
//Bridge crossed
import { UserContext } from "./user";
function Artists () {
//Back across the bridge now with data
const playlist = useContext(PlaylistContext);
const playlistArtists = playlist.map(song => {
return <ArtistTile key={song.id} artist={song.artist} />
}
return(
<div className=playlist-artists>
{playlistArtists}
</div>
)}
export default App
Context
whenever you want to monitor state in your app. Let's explore why this is not such a great choice."No need to worry about having to pass props!"
--You, probably, the first time you heard about context