简体   繁体   English

如何获得Material-UI的 <TextField/> 用ref =&#39;&#39;正确返回 <input/> 在ReactJS中?

[英]How to get Material-UI's <TextField/> to return correctly with ref='' like <input/> in ReactJS?

With the following method: 使用以下方法:

  handleClick(event) {
    const inputText = this.refs.inputText
    console.log(inputText.value.trim())
  }

I am trying to get Material-UI's <TextField/> to return the input text correctly with ref like the <input/> can with <button/> triggering it: 我试图让Material-UI的<TextField/>正确地返回带有ref的输入文本,例如<input/>可以通过<button/>触发它:

  <input
    className='form-control'
    placeholder='Input Text'
    ref='inputText'
    type='text'
  />

  <button
    onClick={(event) => this.handleClick(event)}
  >

And I attempted the following with <TextField/> , but it returns as undefined . 我尝试使用<TextField/> ,但返回的结果为undefined How can I get it to return inputted text correctly like the <input/> above? 我怎样才能像上面的<input/>一样正确返回输入的文本?

  <TextField
    hint='Enter text'
    className='form-control'
    ref='inputText'
    type='text'
  />

I would suggest this approach: 我建议这种方法:

Set up your textfield with a value and onChange function that are hooked into redux itself, where the onChange function just updates the value . 使用连接到redux本身的valueonChange函数设置文本字段,其中onChange函数仅更新value

So you'd have something like this : 所以你会有这样的事情:

   <TextField
       value={this.props.textFieldValue}
       onChange={this.props.textFieldChange}

Where the textFieldChange is an action that simply updates the textFieldValue . 其中textFieldChange是仅更新textFieldValue Most forms in redux will work something like this. redux中的大多数表单都可以这样工作。 Keep in mind the names i made up for those props and action are just for example. 请记住,我为这些道具和动作准备的名字仅是举例。 If you have a big form you might want to consider have part of the state tree dedicated to the form itself where you have : 如果您的表单很大,则可能需要考虑将状态树的一部分专用于表单本身所在的位置:

state: {
    form: {
       textField:   ...your textfield value here,
       name:  ...,
       whateverElse: ...
   }
 };

I like doing this with redux because I can make that architect form part of the state to look like the json payload of wherever I'm sending it to, so there I can just send the form went I want to send it. 我喜欢用redux这样做,因为我可以使该架构师成为状态的一部分,使其看起来像我将其发送到任何地方的json有效负载,因此我可以只发送要发送的表单。

Anyways, back to this example. 无论如何,回到这个例子。 When you click your handleClick now. 单击handleClick时单击立即。 All you need to do is this to get the value: 您需要做的就是获得价值:

handleClick(event) {
   console.log(this.props.textFieldValue.trim());
}

Because the textfield is updated with every change, you always have access to it in your state. 由于文本字段会随着每次更改而更新,因此您始终可以在其状态下访问它。 This also gives you flexibility over the refs approach, because if you use refs you will have a lot harder of a time getting access to that form in other components. 这也使您可以比refs方法更具灵活性,因为如果您使用refs您将很难在其他组件中访问该表单。 With this approach, all the information is on your state so you can access it anytime, as long as you manage your props. 使用这种方法,所有信息都在您的状态上,因此您只要管理道具就可以随时访问。

You should use the onChange={} to get the value: 您应该使用onChange = {}来获取值:

_onChange = (e) => {
    console.log(e.target.value);
}

<TextField
    onChange={this._onChange}
/>

Here's a better solution than using onchange event, we get directly the value of the input created by material-ui textField : 与使用onchange事件相比,这是一个更好的解决方案,我们直接获取material-ui textField创建的输入的值:

  create(e) {
    e.preventDefault();
    let name = this.refs.inputText.input.value;
    alert(name);
  }

  constructor(){
    super();
    this.create = this.create.bind(this);
  }

  render() {
    return (
          <form>
            <TextField ref="inputText" hintText="" floatingLabelText="Your name" /><br/>
            <RaisedButton label="Create" onClick={this.create} primary={true} />
          </form>
    )}

hope this helps. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS + Redux:如何使用Material-UI的RaisedButton作为 <input/> ? - ReactJS + Redux: How to use Material-UI's RaisedButton as <input/>? ReactJS Material-ui TextField应用CSS - ReactJS material-ui TextField apply css 清除文本字段材料-ui ReactJS - Clear Textfield material-ui ReactJS ReactJS + Material-UI:如何在每个TableRow中使用Material-UI的FlatButton和Dialog? - ReactJS + Material-UI: How to use Material-UI’s FlatButton and Dialog in each TableRow? ReactJS和Material-UI:如何仅突出Material-UI内部的内容 <Dialog> 而不是整个页面? - ReactJS & Material-UI: How to highlight just content inside Material-UI's <Dialog> and not the whole page? ReactJS with Material-UI:如何对Material-UI数组进行排序 <TableRow> 按字母顺序排列? - ReactJS with Material-UI: How to sort an array of Material-UI's <TableRow> alphabetically? ReactJS + Material-UI:如何在 ZF8FCA79E4CB15403DA20CE0F0DFB736B 之间交替 colors<table></table> 的<tablerow /> ? - ReactJS + Material-UI: How to alternate colors between Material-UI <Table/>'s <TableRow/>? ReactJS + Material-UI:如何减少 Material-UI 的列宽<TableRow/> ? - ReactJS + Material-UI: How to reduce column width of Material-UI's <TableRow/>? 如何从 material-ui TextField、DropDownMenu 组件获取数据? - How get data from material-ui TextField, DropDownMenu components? React:如何从 Material-UI TextField 组件中获取值 - React: How to get values from Material-UI TextField components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM