javascript - 在 react js 中启用按钮后如何聚焦?
问题描述
我是反应 js 的新手,在完成所有输入字段后,我试图专注于一个按钮,之后,我以编程方式启用该按钮,但我无法专注于它。我也在其实现中使用自动对焦
return (
<div style={{ marginTop: '10%', marginLeft: '25%', backgroundImage: 'linear-gradient(to right, rgba(153,102,255,1), rgba(153,102,255,0.5))', color: '#f3f5f7', height: 'fit-content%', width: '50%', paddingBlockStart: '50px', position: 'fixed' }}>
<form align="center" >
<h1 align="center"> Login</h1><br /><br />
<Grid container direction="row" justify="center" alignItems="center" spacing={4} >
<Grid item >
<TextField input type="number" placeholder={this.props.intl.formatMessage(messages.PlaceHolder)} id="mobileNo" variant="outlined" size="small" required style={{ backgroundColor: 'white', width: '300px', borderRadius: '5px', border: '1px solid' }} onInput={(e) => { e.target.value = Math.max(0, parseInt(e.target.value)).toString().slice(0, 11) }} />
</Grid>
<Grid item >
<MyButton disabled={this.state.validate} variant="outlined" color="primary" onClick={() => this.clickValidate()}> {this.props.intl.formatMessage(messages.ValidateButton)} </MyButton>
<ToastContainer />
</Grid>
</Grid>
<br /><br />
<Grid container direction="row" justify="center" alignItems="center" spacing={4} >
<Grid item >
<ValidationTextField type="number" onInput={this.restrictAlphabets} min={0} required id="otp_1" variant="outlined" size="small" onChange={this.handleChange} inputProps={{ maxLength: 1, size: 1 }} style={{ backgroundColor: 'white', width: '50px', borderRadius: '5px', border: '1px solid' }} />
</Grid>
<Grid item >
<ValidationTextField type="number" onInput={this.restrictAlphabets} min={0} required id="otp_2" variant="outlined" size="small" onChange={this.handleChange} inputProps={{ maxLength: 1, size: 1 }} style={{ backgroundColor: 'white', width: '50px', borderRadius: '5px', border: '1px solid' }} />
</Grid>
<Grid item >
<ValidationTextField type="number" onInput={this.restrictAlphabets} min={0} required id="otp_3" variant="outlined" size="small" onChange={this.handleChange} inputProps={{ maxLength: 1, size: 1 }} style={{ backgroundColor: 'white', width: '50px', borderRadius: '5px', border: '1px solid' }} />
</Grid>
<Grid item >
<ValidationTextField type="number" onInput={this.restrictAlphabets} min={0} required id="otp_4" variant="outlined" size="small" onChange={this.handleChange} inputProps={{ maxLength: 1, size: 1 }} style={{ backgroundColor: 'white', width: '50px', borderRadius: '5px', border: '1px solid' }} />
</Grid>
<Grid item >
<ValidationTextField type="number" onInput={this.restrictAlphabets} min={0} required id="otp_5" variant="outlined" size="small" onChange={this.handleChange} inputProps={{ maxLength: 1, size: 1 }} style={{ backgroundColor: 'white', width: '50px', borderRadius: '5px', border: '1px solid' }} />
</Grid>
<Grid item >
<ValidationTextField type="number" onInput={this.restrictAlphabets} min={0} required id="otp_6" variant="outlined" size="small" onChange={this.handleChange} inputProps={{ maxLength: 1, size: 1 }} style={{ backgroundColor: 'white', width: '50px', borderRadius: '5px', border: '1px solid' }} />
</Grid>
</Grid>
<br /><br /><br />
<MyButton id="login" variant="outlined" aria-disabled={this.state.login} color="primary" onClick={this.handelLogin}> {this.props.intl.formatMessage(messages.Login)} </MyButton>
<br /><br /><br />
<Link style={{ color: 'white' }} onClick={this.handelResendOtp}> {this.props.intl.formatMessage(messages.ResendOtp)} </Link>
<br /><br /><br />
</form>
<div id="regAlert" style={this.state.unregistered ? {} : { display: 'none' }}>
<Alert severity="info"><h3>You are not registered with us, kindly visit www.agoraservices.us for registration!</h3></Alert >
</div>
</div>
以上是一些示例代码,我正在尝试关注登录按钮
解决方案
您可以使用 react 提供的 Ref 来引用任何 HTML 元素,稍后您可以使用它的 current 属性访问它。
https://codesandbox.io/s/friendly-neumann-07wdi?file=/src/App.js
function App() {
const mybtnRef = useRef();
React.useEffect(() => {
mybtnRef.current.focus();
}, []);
return (
<div className="App">
<button ref={mybtnRef} onClick={()=>console.log("hello")}>Click here</button>
</div>
);
}
推荐阅读
- reactjs - React + Firebase 身份验证:在不更改当前登录用户的情况下创建新用户
- amazon-web-services - 雅典娜返回返回零结果
- javascript - Typescript过滤器中回调函数的类型是什么?
- laravel - Laravel Excel:错误资源解释为文档但使用 MIME 传输
- vue.js - 在下面文件的vue JS中获取“在调用reset()时无法读取未定义错误的属性'map'”
- visual-studio-code - 单击“运行代码”时,Visual Studio Code 的 Code Runner 扩展不起作用
- html - 从选择值计算的任何选项
- reactjs - iframe 中的 HTML input type=“file” 会破坏 Chrome 和 Edge 中的文件选择对话框
- scala - 通过名称和调用方法获取类
- ios - 处理两个 CollectionviewCell 之间的拖放