Skip to content

Commit

Permalink
react18 and less reactdom
Browse files Browse the repository at this point in the history
  • Loading branch information
jesuyedavid committed Feb 16, 2024
1 parent 0bf719e commit 51b9e82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
43 changes: 35 additions & 8 deletions src/components/MapboxMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useRef, useEffect } from 'react';
import mapboxgl from 'mapbox-gl';
import { Result, useSearchState } from '@yext/search-headless-react';
import { useDebouncedFunction } from '../hooks/useDebouncedFunction';
import { createRoot } from 'react-dom/client';
import ReactDOM from 'react-dom';

/**
* A functional component that can be used to render a custom marker on the map.
Expand Down Expand Up @@ -136,13 +136,12 @@ export function MapboxMap<T>({
const el = document.createElement('div');
const markerOptions: mapboxgl.MarkerOptions = {};
if (PinComponent) {
const root = createRoot(el!);
root.render(
<PinComponent
index={i}
mapbox={mapbox}
result={result}
/>
renderPinComponentByReactVersion(
el,
i,
mapbox,
result,
PinComponent
);
markerOptions.element = el;
}
Expand Down Expand Up @@ -186,4 +185,32 @@ function getDefaultCoordinate<T>(result: Result<T>): Coordinate | undefined {
return undefined;
}
return yextDisplayCoordinate;
}

function renderPinComponentByReactVersion(
el: HTMLDivElement | null,
i: number,
mapbox: mapboxgl.Map,
result: Result<any>,
PinComponent: PinComponent<any>
) {

if (React.version.startsWith('18')) {
import('react-dom/client').then(({ createRoot }) => {

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-16 / tests (16.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-17 / tests (16.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-16 / tests (18.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-17 / tests (16.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-17 / tests (18.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-16 / tests (16.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-17 / tests (18.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.

Check failure on line 199 in src/components/MapboxMap.tsx

View workflow job for this annotation

GitHub Actions / call_run_tests-react-16 / tests (18.x)

Cannot find module 'react-dom/client' or its corresponding type declarations.
const root = createRoot(el!);
root.render(
<PinComponent
index={i}
mapbox={mapbox}
result={result}
/>
);
});
}else{
ReactDOM.render(<PinComponent
index={i}
mapbox={mapbox}
result={result}
/>, el)
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"declarationMap": true,
"sourceMap": true,
"jsx": "react",
"target": "es2017"
"target": "esnext",
"module": "esnext",
},
"include": [
"src"
Expand Down

0 comments on commit 51b9e82

Please sign in to comment.