小编Bla*_*nny的帖子

为什么 ref 总是为空?

我在下面有一个组件,在第一次渲染时我有正确ref的输入,但是我总是null在下一次渲染时得到 ,请告诉我为什么?ref通过 props 传递到输入:

const inputRef = useRef(null);
useEffect(() => {
  setTimeout(() => {
    if (inputRef.current) {
        inputRef.current.focus();
    }
  });
 }, [inputRef]);

render() {
  return(
    <div>
      <FirstComponent />
      {({ selectedItem, items }) => (
             <SecondCompontnt
                inputRef={inputRef}
                items={items}
                onSelect={onSelect}
                value={selectedItem}
              />
          )}
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

2
推荐指数
1
解决办法
1166
查看次数

标签 统计

javascript ×1

reactjs ×1