Next.js Discord

Discord Forum

Are the context available everywhere

Unanswered
Ninhache posted this in #help-forum
Open in Discord
I mean in client and server components ?
For example :
// app/context/theme.js
'use client';
import { createContext, useContext, useState } from "react";

const ThemeContext = createContext({});

export const ThemeContextProvider = ({ children }) => {
  const [color, setColor] = useState('red');
  return (
    <ThemeContext.Provider value={{ color, setColor }}>
      {children}
    </ThemeContext.Provider>
  );
};

export const useThemeContext = () => useContext(ThemeContext);

Will useThemeContext be usable for any client/server components ? 🤓

4 Replies

Server components cannot consume contexts because contexts are client side states
@joulev Server components cannot consume contexts because contexts are client side states
Any idea to how i could use a global state ?
Basically i could use params but i don't want to put them everywhere, that could fuck the hydratation and more.. :/
global state
is a thing that doesn’t exist on the server
If you want some data you refetch it in the component