Exploring ReactJS and attempting to create a base component called "TextField". If the focus property exists then the field is supposed to set focus after the component mounts. I have the following code but I can't track down why the setting the focus does not work:
import React, { Component } from "react";
import "./inputs.css";
export const TextField = class TextField extends Component {
constructor(props){
super(props);
this.myRef = React.createRef();
}
componentDidMount(){
if(this.props.focus){
this.myRef.current.focus();
}
}
render(){
var errid = this.props.id + "_errmsg";
var labelStyle = "w3-label-font";
var inputStyle = "w3-input w3-border w3-light-grey w3-round";
return(
<div>
<label className={labelStyle}><b>{this.props.label}</b></label>
<input className={inputStyle} type={this.props.type} id={this.props.id} name={this.props.name} ref={this.myRef}/>
<div id={errid} className="error-msg"></div>
</div>
);
};
}