Next.js Discord

Discord Forum

Using useState on page while fetching data from the database

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hello All,

in my page component
i have fetched the data from the database. I want to filter this data and render accordingly.
But I am not able to do this, it throws error like I am not allowed to use the client side on server side .. very confusing.
Any help would be appreciated. I will be adding 3 file i.e.
- file 1 : filter component
- file 2 : one where I have fetched the data from db (where I need this filter bar component with its logic)
- file 3 : where I have used demo data i.e. not from database. But have used the filter bar option with no problem at all.

20 Replies

Cape lionOP
file 3 :
Siamese
You can make all of your components to a client side component
by adding "use client" at the top of each of your files
Cape lionOP
shows this when I add 'use client'
and I was about to use useState
Siamese
Okay, you also need to make all the functions non async
remove async for the functions as well
and do some tirck to lazy load the data you were loding through the APIs
@Siamese Okay, you also need to make all the functions non async remove async for the functions as well
Cape lionOP
this is how I am fetching
// lib/data.ts
import { sql } from '@vercel/postgres';
import { Trek } from './definitions';
import { unstable_noStore as noStore } from 'next/cache';

export async function fetchTreks() {
noStore();
try {
const { rows: treks } = await sql<Trek>SELECT id, name, region, description, duration, difficulty, popularity, max_altitude, image_url FROM treks ORDER BY name ASC;

return treks;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch all treks.');
}
}

export async function fetchTrekByName(trekName: string) {
noStore();
try {
const { rows } = await sql<Trek>SELECT id, name, region, description, duration, difficulty, popularity, max_altitude, image_url FROM treks WHERE name = ${trekName};

const trek = rows[0];

if (!trek) {
throw new Error(Trek with Name ${trekName} not found.);
}

return trek;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch trek by Name.');
}
}
not able to change it to non async function
Siamese
remove the await, do some promise hanlding to fetch the data
@Siamese remove the await, do some promise hanlding to fetch the data
Cape lionOP
this is my new function now
export function fetchTreks() {
noStore();

return new Promise<Trek[]>((resolve, reject) => {
sql<Trek>SELECT id, name, region, description, duration, difficulty, popularity, max_altitude, image_url FROM treks ORDER BY name ASC.then((result: QueryResult<Trek>) => {
const treks = result.rows;
resolve(treks);
}).catch(err => {
console.error('Database Error:', err);
reject(new Error('Failed to fetch all treks.'));
});
});
}
Siamese
yes, that's because you are trying to combine both frontend and backend code in one file
in server compoents you can do that, but in client comopoent you have to seperate the logic
Maybe you can create a /api route
or if you don't want to do that. you can remove all the react hooks in your application or figure out which part of your code is using the the cilent side react hooks and create a separate component for them and mark them as client components
These might help
Cape lionOP
sure thanks. I will try your suggestions : )