Issue with Sanity UI Autocomplete component custom rendering

5 replies
Last updated: Dec 5, 2024
The
Autocomplete
component from
@sanity/ui
crashes with custom rendering.Im trying to implement this:
https://www.sanity.io/ui/docs/component/autocomplete#example-with-custom-rendering The first example works, but the example with custom rendering fails with "Error: Constructor requires 'new' operator".Any ideas?
AI Update

I can see you're hitting the "Constructor requires 'new' operator" error with the Sanity UI Autocomplete component when using custom rendering. While I couldn't locate specific documentation about this exact error in the Sanity UI docs, this error typically occurs in React when there's a component instantiation issue.

Here are the most common causes and solutions to try:

1. Check for React version conflicts

This error often happens when you have multiple versions of React in your project. Check what's installed:

npm ls react

If you see multiple versions, you'll need to deduplicate them. Sanity UI requires React 18+ as a peer dependency, so ensure you're using compatible versions.

2. Verify your component definition

Make sure your custom rendering component is properly defined as a React component:

import {Autocomplete, Card, Text} from '@sanity/ui'

// Define as a proper function component
function CustomOptionRenderer({children}) {
  return (
    <Card padding={3}>
      <Text>{children}</Text>
    </Card>
  )
}

// Use it in renderOption
<Autocomplete
  options={[
    {value: 'option1'},
    {value: 'option2'}
  ]}
  renderOption={(option) => (
    <CustomOptionRenderer>
      {option.value}
    </CustomOptionRenderer>
  )}
/>

3. Try inline rendering first

To isolate whether the issue is with your custom component or the Autocomplete itself:

<Autocomplete
  options={[
    {value: 'option1'},
    {value: 'option2'}
  ]}
  renderOption={(option) => (
    <Card padding={3}>
      <Text>{option.value}</Text>
    </Card>
  )}
/>

If this works but a separate component doesn't, the issue is with how your component is defined or exported.

4. Avoid HTML element names

Don't name your components after native HTML elements like Option, Select, or Input. React may try to instantiate them as DOM elements, causing this error. Use names like CustomOption or AutocompleteOption instead.

5. Check styled-components compatibility

Since Sanity UI uses styled-components as a peer dependency, verify you have a compatible version:

npm ls styled-components

6. Clear build cache

Sometimes stale build artifacts cause this:

rm -rf node_modules package-lock.json
npm install

7. Check the full error stack

Look at your browser console for the complete error stack trace. It should indicate which specific component or line is causing the issue, which can help narrow down the problem.

If none of these solutions work, it would help to see your actual implementation code and the versions of @sanity/ui, react, and styled-components you're using. The full error stack trace would also provide valuable debugging information.

Hi there👋
Is this resolved with the @sanity/ui@2.9.1 update?
Hi
user J
Unfortunately the issue stil remains after I update to 2.9.1.
I figured it out. It's actually the &lt;Text&gt; component which causes the issue.
I figured it out. Its so stupid! it was the &lt;Text&gt; component. And the auto importer in vim mistook it for the
Text()
constructor (https://developer.mozilla.org/en-US/docs/Web/API/Text ). I hadn't properly imported the Text component. 😛
Happy to hear that it’s working now!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?