Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using AddressAutofill in React/Next.JS app does not work #477

Open
starlight-akouri opened this issue Feb 29, 2024 · 11 comments
Open

Using AddressAutofill in React/Next.JS app does not work #477

starlight-akouri opened this issue Feb 29, 2024 · 11 comments

Comments

@starlight-akouri
Copy link

'AddressAutofill' cannot be used as a JSX component.
  Its type 'ForwardRefExoticComponent<AddressAutofillProps & RefAttributes<unknown>>' is not a valid JSX element type.
    Type 'ForwardRefExoticComponent<AddressAutofillProps & RefAttributes<unknown>>' is not assignable to type '(props: any, deprecatedLegacyContext?: any) => ReactNode'.
      Type 'ReactElement<any, string | JSXElementConstructor<any>> | null' is not assignable to type 'ReactNode'.
        Type 'ReactElement<any, string | JSXElementConstructor<any>>' is not assignable to type 'ReactNode'.
          Property 'children' is missing in type 'ReactElement<any, string | JSXElementConstructor<any>>' but required in type 'ReactPortal'.ts(2786)
@galihpermana29
Copy link

galihpermana29 commented Apr 26, 2024

to solve this problem all you do is

npm install @types/react

add this on the tsconfig.json

"paths": { "react": ["./node_modules/@types/react"], }

@thomaslemoine
Copy link

Hi, I have the same error with vitejs, ts, react
Capture d’écran 2024-05-13 à 19 08 05

@galihpermana29
Copy link

Have you tried my solution?

@thomaslemoine
Copy link

Have you tried my solution?

thank you it's fixed

@NateDawg90
Copy link

NateDawg90 commented May 17, 2024

This is also not working for me. My app is Next/React/TS. Error:
Screenshot 2024-05-17 at 10 36 34 AM
My tsconfig, and package.json respectively:
Screenshot 2024-05-17 at 10 36 58 AM
Screenshot 2024-05-17 at 10 37 06 AM

@galihpermana29
Copy link

my tsconfig.json

Screenshot 2024-05-29 at 22 25 52

package.json
Screenshot 2024-05-29 at 22 26 23

I've tried on couple project and it works to me

@sepehr500
Copy link

I am also having this issue. Did exactly what you did above.

@galihsynergy
Copy link

galihsynergy commented Jul 11, 2024

@sepehr500 is that help you or not?

@sepehr500
Copy link

@sepehr500 is that help you or not?

Unfortunately it didn't help

@Marquessmalley
Copy link

Marquessmalley commented Aug 15, 2024

I was able to find a quick fix, was getting the same error trying to use the AddressAutofill component in a react/typescript project.

I believe because the AddressAutofill component doesn't have a type definition, typescript won't allow you to use it.

I created a declarations.d.ts file in my /src folder and added :

declare module "@mapbox/search-js-react" { export const AddressAutofill: React.FC<any>; // Adjust types if more specific ones are available }

hopefully this helps anyone.

@thb
Copy link

thb commented Mar 3, 2025

I'm using import { AddressAutofillRetrieveResponse } from "@mapbox/search-js-core"

Here is a sample code for the full example.

import { useState } from "react"
import { FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { AddressAutofill, AddressMinimap } from "@mapbox/search-js-react"
import { AddressAutofillRetrieveResponse } from "@mapbox/search-js-core"
import { useFormContext } from "react-hook-form"

const MAPBOX_TOKEN = import.meta.env.VITE_MAPBOX_TOKEN

export default function LocationFieldset() {
  const { control } = useFormContext()
  const [minimapFeature, setMinimapFeature] = useState<any>(null)

  function handleAutofillRetrieve(response: AddressAutofillRetrieveResponse) {
    console.log("onRetrieve :", response.features[0]);

    if (response.features && response.features.length > 0) {
      setMinimapFeature(response.features[0])
    }
  }

  return (
    <fieldset className="bg-white p-6 rounded-2xl border shadow-md space-y-4">
      <h2 className="text-xl text-blue-800 font-semibold">Localisation</h2>
      <FormField
        control={control}
        name="address"
        render={({ field }) => (
          <FormItem>
            <FormLabel>Adresse</FormLabel>
            <FormControl>
              <AddressAutofill
                accessToken={MAPBOX_TOKEN}
                onRetrieve={handleAutofillRetrieve}
              >
                <Input
                  placeholder="Entrez votre adresse"
                  autoComplete="address-line1 address-line2"
                  {...{
                    value: field.value,
                    onChange: field.onChange,
                    onBlur: field.onBlur,
                    name: field.name,
                  }}
                />
              </AddressAutofill>
            </FormControl>
            <FormMessage />
          </FormItem>
        )}
      />
      <div className="relative border rounded h-32 w-full lg:max-w-md md:max-w-md" style={{ display: minimapFeature ? "block" : "none" }}>
        <AddressMinimap
          feature={minimapFeature}
          satelliteToggle
          canAdjustMarker
          footer={true}
          accessToken={MAPBOX_TOKEN}
          show={!!minimapFeature}
        />
      </div>
    </fieldset>
  )
}

I think these components are outdated, the rendering is not great. You can easily recreate this feature with mapbox. Just type prompt "a simple address autofill and minimap with mapbox in react typescript" in v0.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants