Proper way to client side fetch data
Unanswered
Cinnamon posted this in #help-forum
CinnamonOP
Is it a antipattern to fetch data from a server action for example to populate a select in a dialog, but only at the time the dialog is opened, so not via RSC. I could encapsulate it inside another RSC but there are conditional inputs that also require api data in the browser so what is the best way to handle that?
import { getListOfShops } from "../lib/actions";
// getListOfShops is a server action
const PublishDialog = () => {
const [isOpen, setIsOpen] = useState(false);
const [data, setData] = useState<unknown>(null);
useEffect(() => {
if (!isOpen) return;
const fetchData = async () => {
const shops = await getListOfShops();
setData(shops);
};
fetchData().catch(console.error);
}, [isOpen]);
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}> this snippet is just as a demonstration2 Replies
I have the same problem and want to find a solution.
Nile Crocodile
its definitely fine. Since a server action turns into an RPC if its called in a client component you can treat server actions as normal fetch requests