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

Fix React SSR hydration ID mismatch error #315

Merged
merged 9 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = {
framework: '@storybook/react',
staticDirs: ['./public'],
webpackFinal: async (config) => {
//use commonjs entry point for "@reach" packages
config.resolve.alias['@reach/auto-id'] = require.resolve('@reach/auto-id');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity why is this necessary?

Copy link
Contributor Author

@yen-tt yen-tt Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our storybook uses commonjs and doesn't seem to handle mjs files.
Screen Shot 2022-10-17 at 12 59 15 PM

@reach/auto-id does export a commonjs version. But its package.json is set to "module": "./dist/reach-auto-id.mjs", and "main": "./dist/reach-auto-id.cjs.js",. I believe Webpack, used by Storybook, by default will try to use "module" over "main". Alternatively, we could also do config.resolve.mainFields = ['main', 'module']; but I thought the alias approach would be more specific.

config.resolve.alias['@reach/utils'] = require.resolve('@reach/utils');

config.resolve.alias['./SearchCore'] = require.resolve('../tests/__fixtures__/core/SearchCore.ts');
config.resolve.alias['../utils/location-operations'] = require.resolve('../tests/__fixtures__/utils/location-operations.ts');
return config;
Expand Down
50 changes: 31 additions & 19 deletions THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,39 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

The following NPM packages may be included in this product:

- @reach/[email protected]
- @reach/[email protected]

These packages each contain the following license and notice below:

The MIT License (MIT)

Copyright (c) 2018-2022, React Training LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

The following NPM package may be included in this product:

- @react-aria/ssr@3.1.0
- @react-aria/ssr@3.3.0

This package contains the following license and notice below:

Expand Down Expand Up @@ -4913,24 +4943,6 @@ OTHER DEALINGS IN THE SOFTWARE.

-----------

The following NPM package may be included in this product:

- [email protected]

This package contains the following license and notice below:

The MIT License (MIT)

Copyright (c) 2010-2020 Robert Kieffer and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

The following NPM package may be included in this product:

- [email protected]
Expand Down
75 changes: 44 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"THIRD-PARTY-NOTICES"
],
"scripts": {
"build": "rm -rf lib/** && npm run build:js && npm run build:css && npm run api-extractor && npm run generate-docs",
"build": "rm -rf lib/** && npm run build:js && npm run build:css && npm run api-extractor && npm run generate-docs && npm run generate-notices",
"build:js": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json",
"build:css": "tailwindcss -o ./lib/bundle.css --minify -c tailwind.config.cjs",
"dev": "tsc --watch -p tsconfig.esm.json",
Expand Down Expand Up @@ -70,7 +70,6 @@
"@types/jest": "^29.1.0",
"@types/lodash": "^4.14.180",
"@types/react": "^17.0.38",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"@yext/eslint-config-slapshot": "^0.5.0",
Expand Down Expand Up @@ -136,6 +135,7 @@
"dependencies": {
"@microsoft/api-documenter": "^7.15.3",
"@microsoft/api-extractor": "^7.19.4",
"@reach/auto-id": "^0.18.0",
"@restart/ui": "^1.0.1",
"@tailwindcss/forms": "^0.5.0",
"@yext/analytics": "^0.2.0-beta.3",
Expand All @@ -144,8 +144,7 @@
"react-collapsed": "^3.3.0",
"recent-searches": "^1.0.5",
"tailwind-merge": "^1.3.0",
"use-isomorphic-layout-effect": "^1.1.2",
"uuid": "^9.0.0"
"use-isomorphic-layout-effect": "^1.1.2"
},
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { DropdownContext, DropdownContextType } from './DropdownContext';
import { InputContext, InputContextType } from './InputContext';
import useRootClose from '@restart/ui/useRootClose';
import { FocusContext, FocusContextType } from './FocusContext';
import { v4 as uuid } from 'uuid';
import { ScreenReader } from '../ScreenReader';
import { recursivelyMapChildren } from '../utils/recursivelyMapChildren';
import { DropdownItem, DropdownItemProps, DropdownItemWithIndex } from './DropdownItem';
import useLayoutEffect from 'use-isomorphic-layout-effect';
import { useId } from '@reach/auto-id';

interface DropdownItemData {
value: string,
Expand Down Expand Up @@ -52,7 +52,7 @@ export function Dropdown(props: PropsWithChildren<DropdownProps>): JSX.Element {
} = props;

const containerRef = useRef<HTMLDivElement>(null);
const screenReaderUUID: string = useMemo(() => uuid(), []);
const screenReaderUUID = useId();
const [screenReaderKey, setScreenReaderKey] = useState<number>(0);
const [hasTyped, setHasTyped] = useState<boolean>(false);
const [childrenWithDropdownItemsTransformed, items] = useMemo(() => {
Expand Down Expand Up @@ -242,7 +242,7 @@ function useDropdownContextInstance(
value: string,
index: number,
focusedItemData: Record<string, unknown> | undefined,
screenReaderUUID: string,
screenReaderUUID: string | undefined,
oshi97 marked this conversation as resolved.
Show resolved Hide resolved
setHasTyped: (hasTyped: boolean) => void,
onToggle?: (
isActive: boolean,
Expand Down
5 changes: 2 additions & 3 deletions src/components/Filters/CheckboxOption.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { FieldValueFilter, Matcher, NumberRangeValue } from '@yext/search-headless-react';
import { useCallback, useEffect, useMemo } from 'react';
import { v4 as uuid } from 'uuid';
import { useFiltersContext } from './FiltersContext';
import { useFilterGroupContext } from './FilterGroupContext';
import { useComposedCssClasses } from '../../hooks/useComposedCssClasses';
import { findSelectableFieldValueFilter } from '../../utils/filterutils';
import classNames from 'classnames';

import { useId } from '@reach/auto-id';
/**
* The configuration data for a field value filter option.
*
Expand Down Expand Up @@ -79,7 +78,7 @@ export function CheckboxOption(props: CheckboxOptionProps): JSX.Element | null {
resultsCount
} = props;
const cssClasses = useComposedCssClasses(builtInCssClasses, props.customCssClasses);
const optionId = useMemo(() => uuid(), []);
const optionId = useId();
const { selectFilter, filters, applyFilters } = useFiltersContext();

const handleClick = useCallback((checked: boolean) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ScreenReader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface Props {
instructionsId: string,
instructionsId?: string,
instructions: string,
announcementKey: number,
announcementText: string
Expand Down
Loading