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?
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.
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.
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.
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.
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.
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.
Instead of Form.Select
, you can use:
<Form.Control as="select">...</Form.Control>
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With