在 React Bootstrap Modal 中获取对 React Select 的引用

Getting a ref to a React Select inside a React Bootstrap Modal

我有如下内容:

import React, { useRef } from 'react';
import Select, { createFilter } from "react-select";
import { Modal } from "react-bootstrap";
function TestModal ({modalProps, selectProps}) {
  const testRef = useRef(null);

 return <Modal {...modalProps}><Select {...selectProps} ref={testRef} /></Modal>

}

testRef 始终为 null - 有人可以解释为什么吗?

关键是Bootstrap Modal documentation中的这句话:“Modals are unmounted when closed.”

您需要添加 show 以安装模态及其子项并初始化引用。