Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't Form.Select recognized in my simple react-bootstrap app, following the react-bootstrap documentation?

I am a "react-bootstrap with hooks" first-timer and I am creating a simple form. I am following the react-bootstrap documentation's example and I am running into a problem with select/option form elements. Regular text input fields render normally without an error. Select statements seem to blow things up.

Here is my code:

import { Form, Button } from "react-bootstrap";

export default function FormComponentName(props) {
    return (
        <>
            <h1>Search Form</h1>

            <Form>

                <Form.Group className="mb-3" controlId="searchState">
                    <Form.Label>State</Form.Label>
                    <Form.Select defaultValue="State...">
                        <option>State...</option>
                        <option value="1">One</option>
                        <option value="2">Two</option>
                        <option value="3">Three</option>
                    </Form.Select>
                </Form.Group>
               <Button variant="primary" type="submit">
                    Search Data
                </Button>

            </Form>


        </>
    )

}

Here is the yarn error message:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `FormComponentName`.

And from the console:

index.js:1 Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check your code at FormComponentName.js:14.
    at FormComponentName

Do I need to install an additional module, or is it something more obvious?

like image 267
MRodriguez Avatar asked Jun 28 '21 16:06

MRodriguez


People also ask

Why Bootstrap JS is not working in react JS?

Why Bootstrap Components Cannot Be Included with React. Adding a HTML tag like <link rel=”stylesheet”/> to an HTML file like index. html is an easy task, but it's not the same when adding Bootstrap to React. The reason is that Bootstrap relies on jQuery to run particular user interface components.

What is form control in React Bootstrap?

The <FormControl> component renders a form control with Bootstrap styling. The <FormGroup> component wraps a form control with proper spacing, along with support for a label, help text, and validation state. To ensure accessibility, set controlId on <FormGroup> , and use <FormLabel> for the label.

Why is form select not working in Bootstrap react?

Form.Select appears to be a beta feature that isn't implemented in current the production version of bootstrap-react. That is the reason why the above code wasn't working. This is a bit useless if true... their documentation does not make this clear at all. I am getting the same error as OP.

How do I combine react and react forms?

We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input.

How do HTML form elements work in react?

HTML form elements work a bit differently from other DOM elements in React, because form elements naturally keep some internal state. For example, this form in plain HTML accepts a single name: This form has the default HTML form behavior of browsing to a new page when the user submits the form. If you want this behavior in React, it just works.

How to submit form as JSON in react-bootstrap?

If a feature isn't working they should not have it in their docs: react-bootstrap.github.io/components/forms/#forms-select In order for "submit" to show it as a json you need to provide the "name" parameter.


Video Answer


3 Answers

Instead of Form.Select, you can use:

<Form.Control as="select">...</Form.Control>
like image 54
stricker88 Avatar answered Oct 20 '22 21:10

stricker88


Form.Select appears to be a beta feature that isn't implemented in current the production version of bootstrap-react. That is the reason why the above code wasn't working.

like image 36
MRodriguez Avatar answered Oct 20 '22 21:10

MRodriguez


Full code will be look like as defined below

 <Form.Group>
    <Form.Label>Status</Form.Label>
    <Form.Control as="select">
      <option>Active</option>
      <option>Inactive</option>
    </Form.Control>
  </Form.Group>
like image 4
shazim ali Avatar answered Oct 20 '22 20:10

shazim ali