简体   繁体   English

使用AntDesign的React / Redux专注于组件

[英]React / Redux with AntDesign focus on component

I'm kind stuck on this feature that i have do implement on my app. 我对我已经在我的应用程序中实现的此功能很满意。 I need to set focus in a component on componentDidMount() or something similar. 我需要将重点放在componentDidMount()或类似的组件上。

i'm already try some suggestions like: 我已经尝试了一些建议,例如:

componentDidMount(){
   this.nameDiv.focus(); 
}

<div ref={(div) => { this.nameDiv = div; }}> 

for focus or similar for (Antdesign component) focus. (Antdesign组件)焦点或类似焦点。

or something like that: 或类似的东西:

document.getElementById("mytext").focus();

<input type="text" id="mytext"/>

usualy i'm receiveing errors on my console: "Cannot read property 'focus' of undefined" 通常,我在控制台上收到错误:“无法读取未定义的属性'focus'”

So the question: How can i set focus on a react / antDesign component? 那么问题来了:我如何集中精力于react / antDesign组件?

Please look to following code. 请查看以下代码。 I'm sure this would help you. 我相信这会对您有所帮助。

import React from "react";
import ReactDOM from "react-dom";

class TestFocus extends React.Component {
    componentDidMount = () => {
        this.refs.focusedInput.focus();
    };

    render = () =>
        <React.Fragment>
            <input placeholder="I don't want focus :/" />
            <br />
            <input ref="focusedInput" placeholder="Please focus on me :B" />
        </React.Fragment>;
}

ReactDOM.render(<TestFocus />, document.getElementById("root"));

Tested with react 16.8.6 & react-dom 16.8.6 测试了react 16.8.6和react-dom 16.8.6

Let me know, if this helps. 让我知道,如果有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM