How to get an input value using “refs” in react-bootstrap form?

前端 未结 7 728
小蘑菇
小蘑菇 2021-02-05 04:01

I\'m trying to create a form that appears in modal. So when user input a value, that value is stored in local storage. Here\'s a picture that help\'s you to understand what I me

相关标签:
7条回答
  • 2021-02-05 04:37

    I think what it suggests to use is the ref callback attribute, so just change inputRef to ref.

    FYI: https://facebook.github.io/react/docs/refs-and-the-dom.html

    0 讨论(0)
  • 2021-02-05 04:41

    I think I found how to get the ref from React-Bootstrap <Form/>.

     import React, {createRef} from 'react'  
    
        interface definedProps {} 
        interface definedState {
                      myRef: Object //I didn't found the more accurate type
                  } 
        export default class SomeClass extends React.Component<definedProps,definedState> {
            state = {
                myRef: React.createRef<Form<any>>() //That's it!
            }
    
        const handleClose = () => {
            console.log('this.state.myRef: ', this.state.myRef); //Here we can take data from Form
            debugger; //todo: remove
        }
    
            render() {
    
                return (
                    <div>
                         <Form ref={this.state.myRef}> { /*Here we're connecting the form's ref to State*/}
                            <Form.Group controlId='formName'>
                                <Form.Control
                                    type='text'
                                    placeholder='Enter Name'
                                />
                            </Form.Group>
                              ...
                            <Button
                                variant='primary'
                                onClick={handleClose}
                                >
                                Save Changes
                            </Button>
                            </Form>
                    </div>
                )
            }
        }
    
    0 讨论(0)
  • 2021-02-05 04:54

    This worked for me, using https://reactjs.org/docs/refs-and-the-dom.html

     constructor(props) {
            super(props);
            this.email = React.createRef();
        }
    
    submit() {
            var email = this.email.current.value;
            console.log(email);
    }
    render() {
       return (
                <Form>
                <Form.Control type="email" placeholder="Your email" ref={this.email} />
                <Button variant="primary" onClick={()=>this.submit()}>Send</Button>
                </Form>
               );
    }
    
    0 讨论(0)
  • 2021-02-05 04:55

    I just made this issue. My code:

    <FormControl
        componentClass="input"
        placeholder="Enter recipe title"
        inputRef={(ref) => {this.input = ref}}
        defaultValue={title}/>
    </FormGroup>
    

    And then you can get the value from <FormControl> in some handler like this:

    console.log(this.input.value);
    

    Details can be found in my repo: https://github.com/kerf007/recipebook

    0 讨论(0)
  • 2021-02-05 04:55

    I have same problem with you, and this is my solution

    const FieldGroup = ({id, label, help, inputRef, ...props}) =>
      <FormGroup controlId={id}>
        <ControlLabel>{label}</ControlLabel>
        <FormControl {...props} inputRef={inputRef}/>
        {help && <HelpBlock>{help}</HelpBlock>}
      </FormGroup>
    

    and my form

    <form>
        <FieldGroup
            id="bookName"
            type="text"
            label="Name"
            placeholder="Enter name..."
            inputRef = {(input) => this.inputName = input }
         />
        <FieldGroup
            id="bookAuthor"
            label="Author"
            type="text"
            placeholder="author 's name..."
            inputRef={(input) => this.inputAuthor = input}
         />
    </form>
    

    then you can get book 's name and author 's name value by:

    this.inputName.value and this.inputAuthor.value
    
    0 讨论(0)
  • 2021-02-05 04:56

    This issue (or more like a change in the way it works) is related to React-Bootstrap. The way you're doing it won't work anymore.

    The <FormControl> component directly renders the or other specified component. If you need to access the value of an uncontrolled <FormControl>, attach a ref to it as you would with an uncontrolled input, then call ReactDOM.findDOMNode(ref) to get the DOM node. You can then interact with that node as you would with any other uncontrolled input.

    Here's an example:

    var React = require('react');
    var ReactDOM = require('react-dom');
    var FormControl = require('react-bootstrap').FormControl;
    
    React.createClass({
      render: function() {
        return (<FormControl ref="formControl" />);
      },
      getFormControlNode: function() {
        // Get the underlying <input> DOM element
        return ReactDOM.findDOMNode(this.refs.formControl);
      }
    });
    

    As soon as you get the DOM element, you will be able to retrieve the value: this.getFormControlNode().value or do anything else that you want.

    PS: Here's a related github issue on this topic.

    0 讨论(0)
提交回复
热议问题