Next.js Discord

Discord Forum

async/await in client component

Unanswered
Rhinelander posted this in #help-forum
Open in Discord
RhinelanderOP
'use client'
import React from "react";
import { useState } from "react";
import SearchBar from "../searchbar/searchbar";
import Display from "../display/display";
import DataFetch from "../display/dataFetch";

export default function HomeBody() {
    const [text, setText] = useState('')
    const [isActive, setActive] = useState(0)
    
    const handleChange = (e) => {
        setText(e.target.value)
    }
    const handleSearchClick = (e) => {
        e.preventDefault()
        setActive(1)
    }
    return (
        <>
            <SearchBar text={text} onChange={handleChange} onSubmit={handleSearchClick}/>
            <Display text={text} isActive={isActive}><DataFetch/></Display>
        </>
    )
}

here I am trying to take the text from the searchbar component and pass it my display component and use the a server component to then make an api request to display the data I was under the assumption that to I needed to pass the server component as a prop for the client component like so here is the code for my display component
export default function Display({ text, isActive, children }) {
    return (
        isActive ? (
            <div>
                {children}
            </div>
        ) : (
            <div />
        )
    );
}

here is my server component
export default async function DataFetch() {
    
    return <div>Server</div>
}

I am not trying to do anything crazy rn just want to understand why this isnt working I thought I am properly passing in the server component as a prop but I keep getting an async await error when the async function is inside a server component

19 Replies

Tonkinese
Display and DataFetch are client components as they are children of HomeBody
@Tonkinese Display and DataFetch are client components as they are children of HomeBody
RhinelanderOP
So I would need to make it a server component?
In order to properly pass datafetch as a prop in the client component display?
Tonkinese
Why does DataFetch need to be a component? Can’t it just be a function in another module?
RhinelanderOP
Yes I could do that, im just wondering if I do it like that does it then need to be called in like an useEffect hook?
so I could then pass response data to a component to then display the data?
Tonkinese
I think the design of the above app isn’t really correct. Try to move things around so HomeBody isn’t a client component. That’s kinda making things difficult atm
Think about component composition / render cycle and move the state into a child component so HomeBody can be a server
@Tonkinese I think the design of the above app isn’t really correct. Try to move things around so HomeBody isn’t a client component. That’s kinda making things difficult atm
RhinelanderOP
The only reason I did that was because I was trying to pass the state from my searchbar component to the datafetch component
I was reading the react documentation and it said to have a parent of both components manage the state between them
so that was what the homebody was for
so it could pass the state of the text to from the seach to the display and then to fetching data
Tonkinese
Yeah but the react docs are different to the next docs
RhinelanderOP
gotcha
Tonkinese
I would personally introduce a context to handle the data. It will give you move freedom with your app
A context does need to be created on the client and client components can only consume the data but you can wrap server components without a problem.
RhinelanderOP
okay I will look into using that will have to read up about it
thanks for your help