简体   繁体   English

React MUI 自动完成“options.filter”不是一个函数

[英]React MUI Autocomplete "options.filter" is not a function

I wish to create a autocomplete search bar with my own custom call to the backend, which searches through a list of tickers.我希望使用我自己对后端的自定义调用创建一个自动完成搜索栏,该调用通过代码列表进行搜索。

<Autocomplete
                multiple
                id="checkboxes-tags-demo"
                options={watchlistSearchTickers}
                disableCloseOnSelect
                getOptionLabel={(option: any) => (option!) ? option.Symbol : null}
                renderOption={(props, option, { selected }) => (
                    <li {...props}>
                        {option.Symbol}
                    </li>
                )}
                style={{ padding: 0 }}
                onChange={(event, query: any) => handleWatchlistSearch(query)}
                filterOptions={(x) => x}
                renderInput={(params) => (
                    <div ref={params.InputProps.ref}>
                        <input type="text" {...params.inputProps} />
                    </div>
                )}
            />

The initial render here seems fine, but on click the text input box, an error "options.filter" is not a function occurs.这里的初始渲染看起来不错,但是在单击文本输入框时,会出现错误"options.filter" is not a function Here is the function that calls the backend through a post request:下面是通过 post 请求调用后端的函数:

const [watchlistSearchTickers, setWatchlistSearchTickers] = useState<Array<watchlistSearchInterface>>([])

function handleWatchlistSearch(query: string) {
        axiosInstance.post("/portfolio/watchlist/search/", {
            query: query
        }).then((res) => {
            console.log(res)
            setWatchlistSearchTickers(res.data)
        })
    }

    useEffect(() => {
        handleWatchlistSearch("")
    }, []) // Initialize with empty list of tickers

Does anyone know why this happens?有谁知道为什么会这样?

So this bug actually happens when you pass null or undefined values into the options prop of the Autocomplete component.因此,当您将nullundefined值传递给 Autocomplete 组件的options属性时,实际上会发生此错误。 This is likely happening because you're clicking in the Autocomplete "too fast", so before it can get the results from your API call, it passes a value that the component tries to .filter() it, causing the error.这很可能发生,因为您“太快”地单击自动完成,因此在它可以从您的 API 调用中获取结果之前,它会传递一个值,该组件尝试将其传递给.filter()它,从而导致错误。

I was also having this issue, and I managed to figure a workaround that works (it's not perfect but it stops your code from crashing).我也遇到了这个问题,我设法找到了一个可行的解决方法(它并不完美,但它可以阻止你的代码崩溃)。

<Autocomplete
            multiple
            id="checkboxes-tags-demo"
            options={!watchlistSearchTickers ? [{label:"Loading...", id:0}] : watchlistSearchTickers }
(...)

With this ternary, what we do is that while your API is working on your request, it puts a placeholder "Loading..." option so it won't crash your code (Similar-ish to the Load on open prop , but I never figured how to make that work).使用这个三元组,我们所做的是,当您的 API 处理您的请求时,它会放置一个占位符“正在加载...”选项,因此它不会使您的代码崩溃(类似于Load on open prop ,但我从来没有想过如何使这项工作)。

For me the solution above works well, but the fanciest and best choice would be the Load prop that is something from MUI.对我来说,上面的解决方案效果很好,但最好和最好的选择是来自 MUI 的Load道具。

Hope I could help!希望我能帮上忙!

Edit: Actually, if you can initialize your options to an empty array [], you can set the loading prop as true, which will display loading while your content loads!编辑:实际上,如果您可以将您的选项初始化为一个空数组 [],您可以将 loading 属性设置为 true,这将在您的内容加载时显示加载!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM