Skip to content

Commit

Permalink
Fix bug in which _props is undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed May 7, 2024
1 parent 901a67a commit 268e40d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/package-lock.json

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

2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "build.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"docs": "node docsServer.js",
"docs": "npm run buildDocs && node docsServer.js",
"buildDocs": "node build.js"
},
"author": "",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

27 changes: 18 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ const ReactSearchInternal = ({

addPreviousSearch(query);
const searchId = ++searchCount.current;
const { searchResults, summary } = await fetchSearchResults(query, isSummaryEnabled);

if (searchId === searchCount.current) {
setSearchResults(searchResults);
setSummary(summary);
setSelectedResultIndex(undefined);
selectedResultRef.current = null;
try {
const { searchResults, summary } = await fetchSearchResults(query, isSummaryEnabled);
if (searchId === searchCount.current) {
setSearchResults(searchResults);
setSummary(summary);
setSelectedResultIndex(undefined);
selectedResultRef.current = null;
}
} catch (error) {
console.log("ReactSearch error:" + error);
}
};

Expand Down Expand Up @@ -397,8 +400,14 @@ class ReactSearchWebComponent extends HTMLElement {
window.customElements.get("react-search") || window.customElements.define("react-search", ReactSearchWebComponent);

export const ReactSearch = (props: Props) => {
_props = props;
// This is magic. If we remove this, then in Console _props somehow ends up
// being undefined when consumed by ReactSearchWebComponent. We found that this
// conditional ensures _props is always set, though it doesn't look like props
// is ever undefined.
if (props) {
_props = props;
}

// @ts-ignore
return <react-search serializedprops={JSON.stringify(props)} />;
return <react-search serializedprops={JSON.stringify(_props)} />;
};

0 comments on commit 268e40d

Please sign in to comment.