【发布时间】:2022-11-09 12:35:00
【问题描述】:
I'm using react-hook-form to handle forms and I encountered an error that only happens on iOS (works fine on android) which tell me that useFormContext is null. The code contains three components so I wrote example on expo which you can find here, as you can see I'm using FormProvider so context should be sent to the children. Anyone has any ideas how to solve it?
Cannot read properties of null (reading 'control')
TypeError: Cannot read properties of null (reading 'control')
at FormSelect
<FormProvider {...methods}>
<Dialog
title={'issue-card'}
body={
<FormSelect
name="card"
label={'card-number'}
options={cards}
/>
}
...
/>
</FormProvider>
export const FormSelect = ({
label,
options,
name,
}) => {
const { control } = useFormContext();
const {
field: { value, onChange },
fieldState: { error },
} = useController({ name, control });
return (
<FormControl>
<Select
onValueChange={onChange}
selectedValue={value}
variant="filled"
_selectedItem={{ background: 'primary.black' }}
>
{options.map((option) => (
<Select.Item
key={`temporary--${value}`}
label={option.number}
value={option.number}
/>
))}
</Select>
</FormControl>
);
};
标签: react-native expo react-hook-form