Parent component has data, but child component does not (inside Dialog).
Unanswered
Noronha posted this in #help-forum
NoronhaOP
Hi guys,
I dont know how to continue debugging this issue.
I have data in the parent component, and when I load it on a "state", it vanishes.
That prints
Now I put a console.log on the SaveablePhoneList component it prints:
PS: I have a similar code on another feature that doesnt use modal, and it works fine. Maybe is related?
I dont know how to continue debugging this issue.
I have data in the parent component, and when I load it on a "state", it vanishes.
export default function DriverForm({ driver, children }: DriverFormProps) {
const [errors, setErrors] = useState<string[] | undefined>(undefined);
let loadedPhones = driver?.phonesToDrivers?.map((phone) => phone.phone);
console.log('loadedPhones');
console.log(loadedPhones);
if (!loadedPhones) {
console.log('no phones');
loadedPhones = [];
}
const [phones, setPhones] = useState<PhoneFormDTOType[]>(loadedPhones);
// other stuff
return (
<Dialog onOpenChange={() => resetForm()}>
<DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>
{driver ? 'Editar Motorista' : 'Novo Motorista'}
</DialogTitle>
</DialogHeader>
<form action={submitFn}>
<FormErrors errors={errors} />
<div className="flex flex-col gap-3">
<Input
required
name="name"
placeholder="Nome do Motorista"
defaultValue={driver?.name}
/>
</div>
<SaveablePhoneList
className="mt-5 w-full"
phones={phones}
setPhones={setPhones}
/>That prints
loadedPhones
driver-form.tsx:35
[{…}]
driver-form.tsx:34 loadedPhones
driver-form.tsx:35
[{…}] Now I put a console.log on the SaveablePhoneList component it prints:
in component
saveable-phone-list.tsx:22
[]PS: I have a similar code on another feature that doesnt use modal, and it works fine. Maybe is related?
3 Replies
Yellow croaker
Could your dialog onOpenChange with resetForm call reset your state if u are directly using those states in your form?
NoronhaOP
Hi, how are you? Good guess, I thought that also @Yellow croaker but it wasn't it.
I put a console.log there and it wasnt reseting the form after I've set the values.
I put a console.log there and it wasnt reseting the form after I've set the values.
NoronhaOP
const resetForm = () => {
console.log('wtf man, who?');
setErrors(undefined);
setPhones([]);
};
// the complete output:
wtf man, who?
driver-form.tsx:34 loadedPhones
driver-form.tsx:35 [{…}]
driver-form.tsx:34 loadedPhones
driver-form.tsx:35 [{…}]
saveable-phone-list.tsx:21 in component
saveable-phone-list.tsx:22 []
saveable-phone-list.tsx:21 in component
saveable-phone-list.tsx:22 []Dont know why it's printing twice...