reactjs – React无法专注于div

当我点击像 here这样的按钮时,我试着把注意力集中在一个div上.我将我的问题减少到下面的代码,虽然在div对象上调用焦点,但是永远不会调用onFocus.此外,autoFocus无法解决我的问题,因为我希望能够在组件安装后多次更改焦点.
class App extends React.Component {
    constructor() {
        super();
        this.my_refs = {};
        this.focusByID.bind(this);
    }

    focusByID(id){
        let myRef = this.my_refs[id];
        if(myRef){
            console.log('focusing on ',id,myRef);
            myRef.focus();
        }
    }
    render() {
        return (
            <div>
                <div
                    id="bigRedDiv"
                    style={{height : 200,width : 200,backgroundColor : 'red'}}
                    ref={(input) => this.my_refs['bigRedDiv'] = input }
                    onFocus={() => console.log('FOCUS IS ON BIG RED DIV')}
                    onClick={() => this.focusByID('bigRedDiv')}
                >
                </div>
            </div>
        );
    }
}

这是包含此代码fiddle.

只是:
...
<div 
    tabIndex="0" //use this attribute
    id="bigRedDiv" 
    style={{height : 200,backgroundColor : 'red'}} 
    ref={(input)=> this.my_refs['bigRedDiv'] = input } 
    onFocus={() => console.log('FOCUS IS ON BIG RED DIV')} 
    onClick={() => this.focusByID('bigRedDiv')} >
</div>
...

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...