New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref not working in React-Bootstrap + TypeScript #598
Comments
This is not a bug. for external controlled component: https://react-hook-form.com/get-started/#ControlledInput 1). custom register, register input during useEffect |
thank you so much |
you are welcome |
thanks @CKGrafico or you can register during useEffect as well
|
Yes, I've read both approaches but the first seems cleaner, do you recommend the second one @bluebill1049? |
I am happy with either :) but it's good to know what's behind the component, which is basically a custom register 👍 |
Hi Guys, I have a similar issue, ref works with react-bootstrap if used like this:
But if used with any option it does not and generates the same error:
I understand that the suggested workaround (react-hook-form-input) works, but I lose much of the clarity and elegance provided by react-hook-form. I wonder why register works without options but not with options. Maybe some clarifications about the difference between register and register({...}) can help. |
@mnesarco can you supply a codesandbox for this? i can take a look why. |
Thank you @bluebill1049 for putting time into this, right now I am finishing a product and have no time to supply the codesandbox example this week. by the way, I used the component and it worked very well without adding to much verbosity.
I will try to come back to this next week and provide the codesandbox. Thank you for this great lib. |
@bluebill1049 Here is condesandobox for issue @mnesarco mentioned. https://codesandbox.io/s/quirky-fire-nxyo6 Using
|
Doesn’t looks like the error is coming from RHF. Maybe try to use controller? |
From what I've tried, it seems that it's only a mismatch between how RHF and React-Bootstrap type a type RBRef = (string & ((ref: Element | null) => void));
<Form.Control type="text" ref={register({ required: true }) as RBRef}></Form.Control> |
Thanks for sharing @frankie567 |
The problem seems to be here: register<Element extends FieldElement = FieldElement>(refOrValidationOptions: ValidationOptions | Element | null, validationOptions?: ValidationOptions): ((ref: Element | null) => void) | void; The return type of register is either |
This seems fixed with |
Just for some clarity for myself with using React bootstrap with RHF
thanks! |
if that works: yes correct usage which means Form.Control expose ref correctly. otherwise at |
I have the same problem with Ionic React. |
watch this? https://www.youtube.com/watch?v=5MsXpmh3Un8 |
// Here a full example import React, { FC, useEffect, useState } from 'react';
import {Row, Col, Card, Form, Button, InputGroup, FormControl} from 'react-bootstrap';
import { useForm } from "react-hook-form";
import { yupResolver } from '@hookform/resolvers/yup';
import * as Yup from 'yup';
const AddUser: FC = ({ history, match }: any) => {
// form validation rules
const validationSchema = Yup.object().shape({
email: Yup.string()
.email('Email is invalid')
.required('Email is required'),
});
// functions to build form returned by useForm() hook
const { register, handleSubmit, reset, setValue, getValues, errors, formState } = useForm({
resolver: yupResolver(validationSchema)
});
return(
<Card>
<Card.Body>
<h5>{ 'Create User'}</h5>
<hr/>
<Row>
<Col>
<Form onSubmit={handleSubmit(onSubmit)} onReset={reset}>
<Form.Row>
<Form.Group as={Col} controlId="formGridEmail">
<Form.Label>Email</Form.Label>
<Form.Control
type="text"
name="email"
ref={register({ required: true })}
className={`form-control ${errors.email ? 'is-invalid' : ''}`}
/>
<Form.Control.Feedback type="invalid">
{errors.email?.message}
</Form.Control.Feedback>
</Form.Group>
</Form.Row>
<Form.Group as={Col} controlId="formControls">
<Button
type="submit"
disabled={formState.isSubmitting}
className="btn btn-primary">
{formState.isSubmitting && <span className="spinner-border spinner-border-sm mr-1">
</span>}
Save
</Button>
</Form.Group>
</Form>
</Col>
</Row>
</Card.Body>
</Card>
);
} |
Describe the bug
If I use ref with register in a
Control
the value is invalid.if instead of
register()
I use() => register()
the code compiles but is not registering the input.Screenshots
The text was updated successfully, but these errors were encountered: