Inside server-side page.tsx
I have client-side component SelectType.tsx
. And should work like this:
- User changes value of select component
- It triggers function inside
page.tsx
- The function should change object called downloadParams, but it doesn’t…
Here is code
page.tsx
import {SelectType} from "@/components/SelectType";
export default async function DownloadWithParams ({ params }: { params: { slug: string } }) {
let downloadParams: DlParameters = {}
const updateDlParams = async (newParams: DlParameters) => {
"use server"
downloadParams = newParams
console.log(downloadParams)
}
return(
<div className="bg-white w-[80%] min-h-[500px] rounded-2xl shadow-lg p-10 text-zinc-800">
<div className="parameters-container w-full px-[100px]">
<form className="flex">
<div className="choosetype-container mr-10 grow-[1] flex flex-col">
<label className="font-semibold block mb-2">Choose a type:</label>
<SelectType dlParams={downloadParams}
updateDlParams={updateDlParams}
/>
</div>
</form>
</div>
</div>
);
}
SelectType.tsx
'use client'
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select";
import {DlParameters} from "@/types";
type HandleFnc = (newParams: DlParameters) => void
export function SelectType({dlParams, updateDlParams} : {dlParams : DlParameters, updateDlParams : HandleFnc}) {
return (
<Select defaultValue={dlParams?.type || ""}
onValueChange={async (value: "video" | "audio") => {
dlParams.type = value || undefined;
await updateDlParams(dlParams);
}}>
<SelectTrigger className="grow border-2 border-gray-300 rounded-2xl font-semibold">
<SelectValue placeholder="Type" />
</SelectTrigger>
<SelectContent className="font-semibold" >
<SelectItem value="video">Video</SelectItem>
<SelectItem value="audio">Audio</SelectItem>
</SelectContent>
</Select>
);
}
Error message
Error: downloadParams is not defined
Source
srcapp[slug]page.tsx (25:9) @ downloadParams
23 | const updateDlParams = async (newParams: DlParameters) => {
24 | "use server"
> 25 | downloadParams = newParams
| ^
26 | console.log(downloadParams)
27 | }
28 |
I just wanna understand why variable is out of the function`s scope and how to make it works