React - How do I display the first value of an array as the default value in a select form?

LoF10

I am trying to display a property of the first object in an array as the default item. When I attempt to submit my form at the moment I get a null value for the selection because I have not engaged it. How would I make sure the value is present? I am using react-bootstrap. Below are the snippets of code representative of this issue:

Here is the focus, I'm not sure what to put in the value field. What I want is to reference the id value and if there is only one item in my array I don't have to open the dropdown, the id will already be there:

<Form.Control as="select" value={WHAT DO I DO HERE??} onChange={props.handleCart}>
   {props.carts.map((cart,i) => <option key={cart.id} value={cart.id}>{cart.cart_name</option>)})                 
</Form.Control>

Home.js

constructor() {
        super();
        this.state = {
    myCarts = [
    {'id':'1', 'cart_name': 'my cart 1'},
    {'id':'2', 'cart_name': 'my cart 2'}
    ],
showMenu: false
}
}

...

Return
<Create
showMenu = {this.state.showMenu}
menuModalToggle={this.menuModalToggle} 
carts = {this.state.carts}
/>

Create.js

const CreateMenuModal = (props) => {
    console.log('props carts there?',props.carts);
    return (
            <Modal
            //dialogClassName={classes.modal}
            show={props.showMenu}
            onHide={props.menuModalToggle}
            >
                 <Modal.Header closeButton>
                    <Modal.Title>Create Menu</Modal.Title>
                </Modal.Header>
                <div style={{ textAlign: 'center', padding: 10 }}>
                    Fill out the following details to create menus for your cart. Once you create a menu you will have the option to add and remove items from that menu.
                    </div>
                <Modal.Body>
                    <Form>
                        <Form.Group controlId="cart_check">
                            <Form.Label>Choose which cart this menu is for:</Form.Label>
                            <Form.Control as="select" value={WHAT DO I DO HERE??} onChange={props.handleCart}>
                            
                                   {props.carts.map((cart,i) => <option key={cart.id} value={cart.id}>{cart.cart_name}</option>)})
                    
                            </Form.Control>
                        </Form.Group>
                        <Form.Group controlId="menu_name">
                            <Form.Label>Enter a name for your menu:</Form.Label>
                            <Form.Control
                                required placeholder="My Menu" name="menu_name" type="email" onChange={props.handleChange} value={props.menu_name} 
                            />
                        </Form.Group>

                    </Form>
                </Modal.Body>
                <Modal.Footer>
                    <Button variant="secondary" onClick={props.menuModalToggle}>Cancel</Button>
                    <Button variant="primary" onClick={props.createMenu}>Add Menu</Button>
                </Modal.Footer>
        
            </Modal>

    )
}

Here is an example of the submit code for reference:

  //CREATE A MENU
        createMenu = () => {
            console.log('create menu with:', this.state.cart_id, this.state.menu_name);
            this.setState({ showMenu: !this.state.showMenu, menuAdded: !this.state.menuAdded });
            let sqlStamp = moment().utcOffset('-0400').format("YYYY-MM-DD HH:mm:ss").substr(0, 18) + '0';
            fetch(
                `${global.api}/addMenu?cart_id=${this.state.cart_id}&menu_name=${this.state.menu_name}&time_joined=${sqlStamp}`,
                { method: "POST" }
            ).catch((error) => {
                console.log(error)
            })
        }
Shyam

Can you try defaultValue form-control-props

<Form.Control
  as="select"
  value={props.carts[0].id.toString()}
  defaultValue={props.carts[0].id.toString()}
  onChange={props.handleCart}
>
  {props.carts.map((cart,i) => (
    <option key={cart.id} value={cart.id}> 
       {cart.cart_name}
    </option>
  ))}
</Form.Control>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I set a default value on a semantic ui react dropdown

分類Dev

How to set a default value in react-select

分類Dev

React Input Form Value In An Array

分類Dev

How to get <Ion-select> to show the default value on form load with reactive form

分類Dev

How do I make the default value 0 with GameKit

分類Dev

I cannot set default value in select list

分類Dev

How do I get first table value in Lua

分類Dev

How do I get the value from form input into a variable

分類Dev

How do I get chrome to use a time value in an html form?

分類Dev

How do I call a function or a method on a select list if the value changes?

分類Dev

How do I to set selected value in a select option using jquery?

分類Dev

If I have a multidimentional array, how can I acsess only the first value of the first dimention?

分類Dev

How to change value of appended select element in a form

分類Dev

How do I set a value as a string React-Native?

分類Dev

how do I find the value stored in array of object and console the key?

分類Dev

How do I force jQuery validation to recheck select/option field value once value has dynamically changed?

分類Dev

for List<String> i want to add first default value as ALL

分類Dev

Angular: How do I return all data for selected item in mat-select on mat-dialog whilst keeping code for selected default value

分類Dev

How do I set a value for a hidden field in a Flask form, using wtf.quick_form?

分類Dev

How to change default value and update with new value in TextInput in react native

分類Dev

anglejs select ng-options default value with another array

分類Dev

how to save checkbox value in array form?

分類Dev

How to insert array value in Implode form in mysql

分類Dev

vue js display obj-array value v-select

分類Dev

How to choose a random Value from Assoc array then display the key and value

分類Dev

First value of array

分類Dev

Angular form checkbox default value

分類Dev

How to format the value of a checkbox Field using React Final Form Field Array?

分類Dev

How to set a default value on a Boolean in a Code First model?

Related 関連記事

  1. 1

    How do I set a default value on a semantic ui react dropdown

  2. 2

    How to set a default value in react-select

  3. 3

    React Input Form Value In An Array

  4. 4

    How to get <Ion-select> to show the default value on form load with reactive form

  5. 5

    How do I make the default value 0 with GameKit

  6. 6

    I cannot set default value in select list

  7. 7

    How do I get first table value in Lua

  8. 8

    How do I get the value from form input into a variable

  9. 9

    How do I get chrome to use a time value in an html form?

  10. 10

    How do I call a function or a method on a select list if the value changes?

  11. 11

    How do I to set selected value in a select option using jquery?

  12. 12

    If I have a multidimentional array, how can I acsess only the first value of the first dimention?

  13. 13

    How to change value of appended select element in a form

  14. 14

    How do I set a value as a string React-Native?

  15. 15

    how do I find the value stored in array of object and console the key?

  16. 16

    How do I force jQuery validation to recheck select/option field value once value has dynamically changed?

  17. 17

    for List<String> i want to add first default value as ALL

  18. 18

    Angular: How do I return all data for selected item in mat-select on mat-dialog whilst keeping code for selected default value

  19. 19

    How do I set a value for a hidden field in a Flask form, using wtf.quick_form?

  20. 20

    How to change default value and update with new value in TextInput in react native

  21. 21

    anglejs select ng-options default value with another array

  22. 22

    how to save checkbox value in array form?

  23. 23

    How to insert array value in Implode form in mysql

  24. 24

    vue js display obj-array value v-select

  25. 25

    How to choose a random Value from Assoc array then display the key and value

  26. 26

    First value of array

  27. 27

    Angular form checkbox default value

  28. 28

    How to format the value of a checkbox Field using React Final Form Field Array?

  29. 29

    How to set a default value on a Boolean in a Code First model?

ホットタグ

アーカイブ