question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get ref to the input?

See original GitHub issue

I’m using this with material-ui like:


class Amount extends Component {
  render() {
    return <NumberFormat {...this.props} decimalScale={2} />
  }
}

render() {
            <TextField
              className={classes.textField}
              fullWidth={true}
              label="Amount"
              name="amount"
              onChange={this.onAmountChange}
              required={true}
              InputProps={{
                startAdornment: (
                  <InputAdornment position="start">$</InputAdornment>
                ),
                inputRef: ref => this.trackRef('amount', ref),
                inputComponent: Amount
              }}
}

But I don’t get a ref to the actual input. How can I get the ref?

Edit: it seem that the omit help function isn’t copying a ref function if one is provided

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
s-yadavcommented, Nov 27, 2017

ref attributes are not passed as props to the component See (https://reactjs.org/warnings/special-props.html). They are internally used by react.

I guess in your Amount component you have to manually pass ref . Something like

<NumberFormat ref={this.props. inputRef} {...resr} decimalScale={2} />

But in this case as well ref will not forward to actual input. I guess I also have to support something similar inputRef like TextField Component.

For now, you can use ReactDOM.findDOMNode(this.amountElm) to find the actual element if you have passed inputRef: ref => this.amountElm = ref

1reaction
mbrevdacommented, Dec 13, 2017

Whoa! This is awesome, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get values from input types using this.refs in reactjs?
1. using ref={ inputRef => this.input = inputRef } · 2. this.name.current.focusTextInput()
Read more >
Refs and the DOM - React
When a ref is passed to an element in render , a reference to the node becomes accessible at the current attribute of...
Read more >
A complete guide to React refs - LogRocket Blog
The first thing we need to do is get a reference for the input: import React, { useRef, useState } from "react"; const...
Read more >
Working with refs in React | CSS-Tricks
How to create a ref ... createRef() is a new API that shipped with React 16.3. You can create a ref by calling...
Read more >
The Complete Guide to useRef() and Refs in React
To make it work you'll need to create a reference to the input, assign the reference to ref attribute of the tag, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found