简体   繁体   English

React hook 表单不适用于来自 reactstrap 的输入

[英]React hook form does not work with input from reactstrap

I have a problem with react-hook-form and reactstrap.我对 react-hook-form 和 reactstrap 有问题。 I have this component List.jsx:我有这个组件 List.jsx:

import { useContext, useEffect } from "react";
import { ListContext, ADD_LIST } from '../providers/ShoppingListProvider';
import { Link } from "react-router-dom";
import { useForm } from 'react-hook-form';
import { ListGroup, ListGroupItem, Form, FormGroup, Button, Input, Label, Container, Row, Col } from 'reactstrap';





export const Lists = () => {
    const [store, dispatch] = useContext(ListContext);
    const { register, handleSubmit, formState: { errors }, getValues } = useForm();
    const onSubmit = data => addList(data);

    const addList = (data) => {
        console.log(data);
        //dispatch({ type: ADD_LIST, store: store, listName: data.name });

    }

    return (
        <Container>
            <Row>
                <Col>

                    <ListGroup>
                        {store.lists.map((item, key) => {

                            return <ListGroupItem key={key} ><Link to={"/list/" + key}>{item.name}</Link></ListGroupItem>
                        })}
                    </ListGroup>
                </Col>
                <Col>
                    <Form onSubmit={handleSubmit(onSubmit)}>
                        <FormGroup>
                            <Label >Name of new list</Label>
                            <Input type="text" placeholder="name of list" {...register("name", { required: true })} ></Input>
                        </FormGroup>
                        <Button type="submit">Přidat list</Button>
                    </Form>
                </Col>
            </Row>

        </Container>
    );

}

the problem is, that when I submit the form, nothing happens (addList won't happen).问题是,当我提交表单时,什么也没有发生(addList 不会发生)。 But when I replace Input (from reactstrap) with normal input from classic html, everything seems working.但是当我用经典 html 的正常输入替换输入(来自 reactstrap)时,一切似乎都在工作。 So the problem is Input from reactstrap.所以问题是来自 reactstrap 的输入。 Does anyone knows how to fix this issue?有谁知道如何解决这个问题? Thank you very much!非常感谢!

Try like this, look at the innerRef in input试试这样,看输入中的innerRef

const { register, handleSubmit, formState: { errors } } = useForm();
const { ref, ...rest } = register('name')
const onSubmit = data => console.log(data);
return (
    <form onSubmit={handleSubmit(onSubmit)}>
        <Input type="text" placeholder="Name" innerRef={ref} {...rest} />

        <Input type="submit" />
    </form>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM