簡體   English   中英

我正在嘗試從父組件(https://react-select.com/home)重新獲取 React-Select 的 AsyncSelect

[英]I am trying to Reast React-Select's AsyncSelect from a parent component (https://react-select.com/home)

我正在嘗試從父組件(Hooks)重置 AsyncReact( https://react-select.com/home ),但我似乎無法讓它工作。 非常感謝任何幫助。

我可以通過將狀態設置為空數組來從組件內部重置它,但我似乎無法從外部重置它。

父組件

const MessengerMessagesTwo = ({ props }) => {
    
   const orgSearchRef = useRef(null);

   
   

 const handleClearOrgSearch = () => {
        orgSearchRef.current.clearValue();
    }

 return <div>

 <button onClick={() => handleClearOrgSearch()}>clear</button>

 <MessengerMessagesTwoOrgSearch ref={orgSearchRef} />
</div>
})

子組件




const MessengerMessagesTwoOrgSearch = forwardRef(({props}, ref) => {

    const [inputValue, setInputValue] = useState([]);

    useEffect(() => {
        setInputValue([]);

        return () => {
            setInputValue([]); // reset orgs selected
            handleOrgSelectedCallback([]);
        }
    }, [])

    return <div>
        <AsyncSelect
            isClearable
            defaultOptions
            placeholder="Search Orgs"
            loadOptions={loadOptions}
            onInputChange={handleInputChange}
            onChange={handleChange}
            isMulti
            noOptionsMessage={i => "Search..."}
            ref={ref}
        />
    </div>
})


const mapStateToProps = (state) => {
    return {
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        
    };
};

export default connect(mapStateToProps, mapDispatchToProps, null, {forwardRef: true})(MessengerMessagesTwoOrgSearch);

似乎現在正在工作。 什么都沒有改變,我很困惑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM