#async/await in client component

20 messages · Page 1 of 1 (latest)

spare cloud
#
'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

shut lichenBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

pseudo phoenix
#

Display and DataFetch are client components as they are children of HomeBody

spare cloud
#

In order to properly pass datafetch as a prop in the client component display?

pseudo phoenix
#

Why does DataFetch need to be a component? Can’t it just be a function in another module?

spare cloud
#

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?

pseudo phoenix
#

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

spare cloud
#

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

pseudo phoenix
#

Yeah but the react docs are different to the next docs

spare cloud
#

gotcha

pseudo phoenix
#

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.

spare cloud
#

okay I will look into using that will have to read up about it