Material-ui: I can't access value from textField component using refs

Created on 16 Jul 2016  路  7Comments  路  Source: mui-org/material-ui

I have been trying to build a simple form to record user input from the textField component by using a ref but every time I try to reference the ref, I get a null return. Can someone show me how to record a value using refs?

Note: I already implemented a solution using states but ran into problems with pulling in data from containers so I need a solution using refs. Thank you!

Here is my code:

class Form extends React.Component{

  handleSubmit(event){
    event.preventDefault();
    var name = (this.refs.name).value;
    console.log(name);
            if (!name) {
              return;
            }
            // TODO: send request to the server
            (this.refs.name).value = '';
            return;
    }
  render() {
    return (
      <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
        <div>
        <TextField
          ref="name"
          hintText={"Enter Your Name"}
          floatingLabelText={"Name"}
        />
          <RaisedButton type="submit" label="Submit" onClick={this.handleSubmit.bind(this)} />
        </div>
      </MuiThemeProvider>
    )

  }

}

Most helpful comment

@nickhallph Use the inputRef property or follow our FAQ.

All 7 comments

@yonidej That doesn't look like an issue with this lib. I'm closing it. Would be better to post your question on StackOverflow, the answer would help more people.

I'm also having the same issue and it only happens when using material-ui. @yonidej Did you got an answer from stack overflow?

Just found the solution to getting the value : http://stackoverflow.com/questions/31446751/how-to-get-password-field-value-in-reacts-material-ui . Any idea how to set the value?

Also found how to set the value! The method we used earlier email.value won't work. Use this instead email.input.value = ''; (Replace email with the ref name)

This is the top result for searching how to get a value from a mui textfield, so let's just leave it and actually use this post as it was intended, to help people.

@nickhallph Use the inputRef property or follow our FAQ.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

newoga picture newoga  路  3Comments

ryanflorence picture ryanflorence  路  3Comments

activatedgeek picture activatedgeek  路  3Comments

revskill10 picture revskill10  路  3Comments

FranBran picture FranBran  路  3Comments