如何使用" refs"获取输入值在react-bootstrap形式?

时间:2016-12-11 10:32:47

标签: reactjs react-bootstrap

我试图创建一个以模态显示的表单。因此,当用户输入值时,该值存储在本地存储中。这是一张帮助您了解我的意思的图片:
enter image description here 以下是表单的代码:

function FieldGroup({id, label, help, ...props}) {
    return (
        <ReactBootstrap.FormGroup controlId={id}>
            <ReactBootstrap.ControlLabel>{label}</ReactBootstrap.ControlLabel>
            <ReactBootstrap.FormControl {...props} />
            {help && <ReactBootstrap.HelpBlock>{help}</ReactBootstrap.HelpBlock>}
        </ReactBootstrap.FormGroup>
    );
}

const formInstance = (
    <form>
        <FieldGroup
            id="formControlsText"
            type="text"
            label="Text"
            placeholder="Recipe Name"
            inputRef={ref => { this.input = ref; }}
        />
        <ReactBootstrap.FormGroup controlId="formControlsTextarea">
            <ReactBootstrap.ControlLabel>Ingredients</ReactBootstrap.ControlLabel>
            <ReactBootstrap.FormControl componentClass="textarea" placeholder="Enter Ingredients" />
        </ReactBootstrap.FormGroup>
    </form>
);  

正如我在引导程序React教程中读到的那样,我应该添加
<FormControl inputRef={ref => { this.input = ref; }} />到FormControl道具。但添加它后,我在调用模态窗体时出错:

`enter image description here

7 个答案:

答案 0 :(得分:27)

我刚刚提出这个问题。我的代码:

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

然后你可以在某个处理程序中从<FormControl>获取值:

console.log(this.input.value);

详情可在我的仓库中找到:https://github.com/kerf007/recipebook

答案 1 :(得分:8)

我和你有同样的问题,这是我的解决方案

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

和我的表格

<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>

然后您可以通过以下方式获取图书的名称和作者姓名值:

this.inputName.value and this.inputAuthor.value

答案 2 :(得分:4)

这个问题(或者更像是工作方式的改变)与React-Bootstrap有关。你正在做的事情已经不再适用了。

<FormControl>组件直接呈现该指定组件或其他指定组件。如果您需要访问不受控制的<FormControl>的值,会像对待不受控制的输入一样附加引用,然后调用ReactDOM.findDOMNode(ref)以获取DOM节点。然后,您可以像处理任何其他不受控制的输入一样与该节点进行交互。

以下是一个例子:

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);
  }
});

一旦获得DOM元素,您就可以检索值this.getFormControlNode().value或执行您想要的任何其他操作。

PS:这个主题是related github issue

答案 3 :(得分:1)

我认为它建议使用参考回调属性,所以只需将inputRef更改为ref

仅供参考:https://facebook.github.io/react/docs/refs-and-the-dom.html

答案 4 :(得分:0)

您好,此解决方案对我有用!

<Form
  noValidate
  validated={validated}
  onSubmit={(e) => this.handleSubmit(e)}
  style={{ width: '100%' }}
>
  <Form.Group controlId="formBasicEmail">
   <Form.Label>Email address</Form.Label>
   <Form.Control type="email" placeholder="Enter email" inputRef={(ref) => { this.email = ref }} required />
   <Form.Text className="text-muted"> Well never share your email with anyone else.
   </Form.Text>
  </Form.Group>
</Form>

handleSubmit(event) {
    console.log(event.target.elements.formBasicPassword.value)
}

答案 5 :(得分:0)

这对我有用,使用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>
           );
}

答案 6 :(得分:0)

我想我找到了如何从React-Bootstrap ref中获得<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>
            )
        }
    }