Bad ways you’re using react – React Context API

Share This Post

Ayo React fam! We all know that React Context API is the real MVP when it comes to sharing state and data across the court. But watch out, cause sometimes we might slip up, and our app ends up benched. In this post, we’re gonna break down some common fouls when using React Context API, and how to stay on top of our game.


What is the Context API

Context API is like that secret sauce in React that helps you pass the rock (I mean, data) across your components without all that prop-drilling nonsense. It’s clutch when you got global state or some data that lots of components need to get their hands on.


With Context API, you create a context, and then you got a Provider and a Consumer. The Provider’s like your point guard, dishing out the data, and the Consumer’s like your team, catchin that data and usin it to make plays.


So when you need to share some data all over your app, just use Context API to make it happen smooth and easy, without making your components do all that extra work. It’s a game-changer, for real! 🏀💯


3 ways devs misuse the Context API

Doing too much: Don’t be that ball hog who uses Context for every lil state in the app. Context shines for that global state or when many components need that alley-oop. But if you overdo it, you’ll be stuck with unnecessary re-renders and slowed-down plays.


Cramming Contexts together: Piling up Contexts like they’re your sneaker collection? Nah, that’s gonna mess up your code, making it hard to read and maintain. Don’t get lost in that Context maze. The game plan? Combine those related Contexts into one or use custom hooks to keep it clean and organized.


Usin Context when it ain’t necessary: Sometimes devs get all hyped up about Context and start usin it when there’s a simpler way to pass data around. Remember, Context is perfect for that global state or when a bunch of components need access. But if you’re just passin data down one or two levels, you might as well stick with props. Keep it simple and avoid overcomplicating your code, fam!


An Example

Aight, let’s break it down with an example. Imagine you’re building a fire mixtape app and you got components for Album, TrackList, and Track. You need to pass down the “playbackStatus” state from the Album all the way to the Track components. Check this out:


The Bad Way (without Context):

function Album() {
  const [playbackStatus, setPlaybackStatus] = useState('paused');
  return <TrackList playbackStatus={playbackStatus} setPlaybackStatus={setPlaybackStatus} />;

function TrackList({ playbackStatus, setPlaybackStatus }) {
  // Render a bunch of tracks
  return <Track playbackStatus={playbackStatus} setPlaybackStatus={setPlaybackStatus} />;

function Track({ playbackStatus, setPlaybackStatus }) {
  // Use playbackStatus and setPlaybackStatus here

Here, we’re passin’ the playbackStatus state from Album to Track through TrackList. But that’s prop drilling, and it ain’t cool. Now, let’s level up and use Context to make it smooth:


The Better Way (with Context):

const PlaybackStatusContext = createContext();

function Album() {
  const [playbackStatus, setPlaybackStatus] = useState('paused');
  return (
    <PlaybackStatusContext.Provider value={{ playbackStatus, setPlaybackStatus }}>
      <TrackList />

function TrackList() {
  // Render a bunch of tracks
  return <Track />;

function Track() {
  const { playbackStatus, setPlaybackStatus } = useContext(PlaybackStatusContext);
  // Use playbackStatus and setPlaybackStatus here

Now that’s what’s up! By using Context, we’re sharing that playbackStatus state without prop drilling. The TrackList component ain’t even bothered with the state it doesn’t need. Your code’s cleaner, more organized, and your components are vibin. Keep ballin’!


Leave a Reply

Digital art dealers

Join our vibrant community of more than 750 members by subscribing to our newsletter. Every Monday, you'll receive a programming meme to kickstart your week.

follow us on

© 2023 Digital Art Dealers. All rights reserved.

%d bloggers like this: