Next.js Discord

Discord Forum

usecontext in the server component

Unanswered
Bengal posted this in #help-forum
Open in Discord
BengalOP
I'm getting this error:
Attempted to call useBoardContext() from the server but useBoardContext is on the client.
The useBoardContext is a client component that uses usecontext And the component that throwes this error is a server component,
can I save the parent component to be server component and use in some how the usecontext function?

this is the useBoardContext
'use client'

import { BoardContext } from "../context/boardContext";
import { useContext } from "react";

export const useBoardContext = () => {
  const context = useContext(BoardContext);

  if (!context) {
    throw Error('useBoardContext must be used inside an BoardContextProvider');
  }

  return context;
}

and this is the server component
import React from 'react';
import { useBoardContext } from '@/board/hooks/useBoardContext';

function list() {

  const { board } = useBoardContext();

  return (
    <>  
      {
        board?.lists?.map((items, droppableIndex) => (
        <Droppable id={"drop-"+droppableIndex} className="droppable" key={items._id} initialItems={items} index={droppableIndex}>
          {(innerRef) => (
            <>
            </>
          )}
        </Droppable>
      ))}
    </>
  );
}

14 Replies

import { useBoardContext } from '@/board/hooks/useBoardContext'; you cant call it here
should be client component
BengalOP
what does it means there isnt why to call usecontext from server component?
useContext is a client component
everything with use is a client component
'use'
is board is something that you fetch from a end point ?
BengalOP
yes
if it is a get route you can call multiple times
it wont call from database each time. utilize the cache
BengalOP
yes but I want to use reducer also, cause I make it move the cards in the list
okay soo if that data dont require to be usefull for the seo or like that then you can use swr library to fetch and mutate the data. also make the componenets that is using useContext or anyhooks as a client compoennt
the thing i meant as leaf is that. if there is a user interactivity in anyof the component make the part of the component as seperate component and make it client component