繁体   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