首页 > 解决方案 > 为什么我的 React ref 没有安装在 map 函数中?

问题描述

我正在使用这条线使用 ref 回调创建 refs

    setRef = (ref) => {
        this.inputRefs.push(ref);
      };

这是我的渲染函数,其中添加了引用

render() {
        return (
            <div onClick={(e) => { this.removePopUpBox(e) }}>
                <div> {this.state.popUpBox ? <PopUpBox /> : null} </div>
                <div className="add" onClick={this.addPopUpBox}>+</div>
                <div className="tasks-wrap">
                    {this.state.items.map((task, index) => {
                        return (
                            <Item ref={this.setRef} index={index} key={index} item={task} remove={this.remove}/>
                    )})}
                </div>


            </div>);
    }

这是我的构造函数

 constructor(props) {
        super(props);
        this.inputRefs = []
}

出于某种原因,我的 refs 已初始化但没有安装到元素上,因此对 current 的任何调用都返回为未定义。谢谢!

标签: javascriptreactjs

解决方案


推荐阅读