#Managing state of data fetched with Prisma.

1 messages · Page 1 of 1 (latest)

urban sentinel
#

Im trying to get data from database with Prisma, but then those data has to be managed in some sort of state (compatible with app router).

component for fetching data:

import prisma from '@/prisma/client';
import React from 'react';

async function GetGamers() {
    const gamers = await fetchGamers();
    return (
        gamers
    );
}
async function fetchGamers() {
    const gamers = await prisma.gamers.findMany();
    return gamers;
}
export default GetGamers;```
component wrapping data as an option for select:
```import React from 'react';
import GetGamers from  './GetGamers'
async function GamersList() {
    const gamersJsx = await GetGamers();
    return (
        <>
            {
                gamersJsx.map((gamer) =>
                    (<option key={gamer.id}>{gamer.name}</option>))
            }
        </>
    );
}

export default GamersList;```
component presenting data:
```import React from 'react';
import GamersList from './GamersList'

interface Gamer {
    id: number;
    lastTen: string;
    mmr: number;
    name: string;
    server: string;
}

interface Gamers {
    gamers: Gamer[];
}

 function PickGamersBar() {
   
    const gamersTable: string [] = new Array(10).fill({});
    /


     const gamersJSX = gamersTable.map((elem, index) =>
        (
            <select key={index}
                    className="select select-ghost select-bordered w-full max-w-xs flex flex-col space-y-4 text-white"
            >
                <option disabled selected>-Pick_Gamer-</option>
                {/* @ts-expect-error Server Component */}
                <GamersList/>
            </select>
        )
    );

    return (
        <nav className="absolute left-0 right-0 p-4 h-3/4 flex flex-col justify-center">
            <div className="container mx-auto ">
                {gamersJSX}
            </div>
        </nav>
    );
}


export default PickGamersBar;```
Now I want to make that picking a player with disable its choice in other selects.
ocean fractalBOT
#

Hi @urban sentinel. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems you are trying to manage state in a Next.js application using Prisma for data fetching. However, the extracts do not provide specific information on managing state for data fetched with Prisma in a Next.js application.

In general, state management in React (and therefore Next.js) can be achieved using various methods such as using React's built-in useState and useContext hooks, or external libraries like Redux or MobX. For your specific case, you might want to consider using React's built-in state management hooks.

However, it's important to note that state management strategies can vary widely based on the specific requirements of your application, and the best solution may depend on factors not mentioned in your question.

Unfortunately, without more specific information or examples related to state management with Prisma in the provided extracts, it's difficult to provide a more detailed or actionable answer.

For more information on state management in Next.js, you might find the Next.js Documentation (https://nextjs.org/docs/basic-features/data-fetching) helpful. For more details on using Prisma with Next.js, the Prisma Documentation (https://www.prisma.io/docs/concepts/components/prisma-client/crud) might be of use.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@urban sentinel kapa.ai is still learning and improving, please let us know how it did by reacting below

urban sentinel
#

@ocean fractal is there a rule on how should server client component should be ordered? for example is it acceptable practice that client component is on top and server components are nested?

ocean fractalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, there are certain rules and recommendations when it comes to ordering and nesting Server and Client components in Next.js.

When interleaving Client and Server Components, it's helpful to visualize your UI as a tree of components. Starting with the root layout, which is a Server Component, you can then render certain subtrees of components on the client by adding the "use client" directive. Within those client subtrees, you can still nest Server Components or call Server Actions.

However, there are some things to keep in mind:

  • During a request-response lifecycle, your code moves from the server to the client. If you need to access data or resources on the server while on the client, you'll be making a new request to the server - not switching back and forth.
  • When a new request is made to the server, all Server Components are rendered first, including those nested inside Client Components. The rendered result (RSC Payload) will contain references to the locations of Client Components. Then, on the client, React uses the RSC Payload to reconcile Server and Client Components into a single tree.
  • Since Client Components are rendered after Server Components, you cannot import a Server Component into a Client Component module (since it would require a new request back to the server). Instead, you can pass a Server Component as props to a Client Component.

Here's an example of a supported pattern where a Server Component is passed as a prop to a Client Component:

// ClientComponent
'use client'
import { useState } from 'react'
export default function ClientComponent({ children }) {
 const [count, setCount] = useState(0)
 return (
 <>
 <button onClick={() => setCount(count + 1)}>{count}</button>
 {children}
 </>
 )
}
#

// Page
import ClientComponent from './client-component'
import ServerComponent from './server-component'
export default function Page() {
return (
<ClientComponent>
<ServerComponent />
</ClientComponent>
)
}


In this case, `<ClientComponent>` and `<ServerComponent>` are decoupled and can be rendered independently. The child `<ServerComponent>` can be rendered on the server, well before `<ClientComponent>` is rendered on the client.

For more details, you can refer to the **Next.js Documentation on Server and Client Composition Patterns** (<https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@urban sentinel kapa.ai is still learning and improving, please let us know how it did by reacting below

urban sentinel
#

@ocean fractal can client component be async?

ocean fractalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

No, client components in Next.js cannot be async functions. This is in accordance with the React Server Component RFC on promise support (https://github.com/acdlite/rfcs/blob/first-class-promises/text/0000-first-class-support-for-promises.md).

If you have an async function in a client component, you will need to either:

  1. Remove the async keyword from the client component function declaration, or
  2. Convert the client component to a server component.

For more details, you can refer to the Next.js Documentation on No Async Client Component (https://nextjs.org/docs/messages/no-async-client-component).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: