problem with use Client
Unanswered
Challangerson posted this in #help-forum
'use client'
import React from 'react'
import style from '../../css/user.module.scss'
import { useState, useEffect } from 'react'
import UserSide from "../../../../components/UserSide.js"
export async function GET({params}) {
const [user, setUser] = useState([]);
useEffect(()=>{
fetch("../../api/user/" + params.user)
.then((res) => res.json())
.then((user) => {
setUser(user);
})
},[])
return(
<>
<div>
XD
</div>
</>
)
}86 Replies
⨯ Error: Attempted to call GET() from the server but GET is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.@Challangerson js
'use client'
import React from 'react'
import style from '../../css/user.module.scss'
import { useState, useEffect } from 'react'
import UserSide from "../../../../components/UserSide.js"
export async function GET({params}) {
const [user, setUser] = useState([]);
useEffect(()=>{
fetch("../../api/user/" + params.user)
.then((res) => res.json())
.then((user) => {
setUser(user);
})
},[])
return(
<>
<div>
XD
</div>
</>
)
}
Source of this file SRC/app/user/[user]/route.js
why are you trying to make a route handler with client component?
you should just remove
'use client'?Then its send useEffect needs use client
wait i think you missunderstood route handler
this should just be a page.tsx
So error said u have a bad file
route handers are not react
Or not found route.js
page files are react and can have useEffect
@Challangerson Or not found route.js
yes that is react
If i back too home i will change it and said if i still has errror
im confused... why aren't you doing this code in a
page.tsx?Cuz i builded it in src/app
route handlers are for returning arbitrary data and not jsx
@Challangerson Cuz i builded it in src/app
yeah, you use page.tsx in app dir
And not using jsx cuz i hate jsx and always typed any
And all said if i always typed any i need to change it to js
yeah i should have been more specific that i meant the fancy react syntax for making components
What is your purpose of the above code? Can you elaborate more about your question? (e.g. what are you trying to do?)
Getting user from database then print info about him
From Minecraft guilds
print info about himSo what do you expect? A html page? or just an ordinary Rest API response?
Cuvier’s Dwarf Caiman
Here u should rename your file from route.js to page.js and in ur code everything is good so uve just check your api
for a html page, use [
Please check the docs before asking
page.tsx](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts)Please check the docs before asking
read through the whole section for routing
also fix your fetch req to not have
../../ i believe@riský also fix your fetch req to not have `../../` i believe
Cuvier’s Dwarf Caiman
Exactly, if he encounter an error it's mean from api call
Ash-throated Flycatcher
Your code is all wrong, a client component cannot be async, and if you want to use a server component, it cannot have state and useEffect.
@fuma for a html page, use [`page.tsx`](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts)
Please check the docs before asking
The default export is not a React Component in page: "/user/[user]"
@riský
changed name to page.js
and still that same problem
AbortError: The user aborted a request.can you show your latest code for the file
'use client'
import React from 'react'
import style from '../../css/user.module.scss'
import { useState, useEffect } from 'react'
import UserSide from "../../../../components/UserSide.js"
export function GET({params}) {
const [user, setUser] = useState([]);
useEffect(()=>{
fetch("/api/user/" + params.user)
.then((res) => res.json())
.then((user) => {
setUser(user);
})
},[])
return(
<>
<div>
XD
</div>
</>
)
}you need default function here
export default function PageThingy
⨯ Detected default export in 'C:\Users\MazurKacper\Desktop\SalkMac\website\src\app\api\user\[user]\route.js'. Export a named export for each HTTP method instead.but what is that error?
you are using route.js still
not page that you claimed
uhh hmm
or in sql request cant ask for route?
try to delete .next folder
ohh yeah this was for the api
but do you want an API (returnes text or json) or a page (react html)?
@Challangerson Click to see attachment
this folder return jsx
yes thats not an api
@Challangerson Click to see attachment
in that folder
make that file default export
and have the api as
'use client'
import React from 'react'
import style from '../../css/user.module.scss'
import { useState, useEffect } from 'react'
import UserSide from "../../../../components/UserSide.js"
export function page({params}) {
const [user, setUser] = useState([]);
useEffect(()=>{
fetch("/api/user/" + params.user)
.then((res) => res.json())
.then((user) => {
setUser(user);
})
},[])
return(
<>
<div>
XD
</div>
</>
)
}
export default pageand capitol P in page for function name (as react be like that)
thats the structure of my app
so whats the error now
thats the full console
yes in route.js you use the
export function GETðŸ‘
as default export in api