我正在使用syncfusion反應控件向我的應用程序添加一些功能。我想在我的功能組件中調用控件上的方法,但我無法ref正確獲取設置:import React, {createRef, useEffect, useState} from "react";import {AutoCompleteComponent} from "@syncfusion/ej2-react-dropdowns";import "@syncfusion/ej2-base/styles/bootstrap.css";import "@syncfusion/ej2-react-inputs/styles/bootstrap.css";import "@syncfusion/ej2-react-dropdowns/styles/bootstrap.css";const UserLookup = ({userSelected}) => { const [searchString, setSearchString] = useState(''); const [items, setItems] = useState([]); const helper = new QueryHelper(); let listObj = createRef(); const searchStringChanged = (args) => { console.log(args.text); if (args.text.length > 3) { setSearchString(args.text); } } const optionSelected = (event) => { memberSelected(event.item.id); } useEffect(() => { fetch('http://example.com/myendpoint') .then(response.map((result) => { listObj.current.showPopup(); // <-- this method should be called on the autocomplete component return { id: result.contactId, label: result.firstName + ' ' + result.lastName } })) .then(data => console.log(data)); }, [searchString]); return ( <AutoCompleteComponent id="user_search" autofill={true} dataSource={items} fields={ { value: 'label' } } filtering={searchStringChanged} select={optionSelected} popupHeight="250px" popupWidth="300px" placeholder="Find a contact (optional)" ref={listObj} /> );};export default UserLookup;這總是會引發一個錯誤Cannot read property 'showPopup' of null- 如何設置 實例的 refAutoCompleteComponent以便可以調用它的方法?
通過ref調用React功能組件中子組件的方法
12345678_0001
2023-07-20 16:27:32