Unable to focus using ref
I'm trying to focus on DynamicNumber, but it throws error as cannot read focus of null. I'm targeting from onChange of another component. Simple example of what I have done:
xyz = () => { this.abc.focus(); }
<DynamicNumber ref={(input) => { this.abc = input; }} /> <def onChange={this.xyz} />
Hey Thank you very much for your involvement.
I updated documentation to show how to use focus on element. In examples first element also has focus.
Please check it out. In addition I also set focus on first element in examples : http://htmlpreview.github.io/?https://github.com/uhlryk/react-dynamic-number/blob/master/example/index.html
You can try it.
There are few posibilities why you get error (without seeing your component it may be hard to say). I think it is because you are using stateless components. For ref your component should be a class.
Thank you @uhlryk . I have tried with both class and const, but it throws the same error. Instead of defining focus on componentDidMount() I'm defining this.input.focus for some other components onChange function. Could you please try if this scenario works well for you.
Hey, Im eager to solve this! Lets try first: Replace DynamicNumber with standard input (of course adapt all code for this) and check if you will get ref to input. Second try : prepare simplest basic component which shows this problem and show it here. I will run it and try to solve problem