Hi all!
I’m trying to read from two tables using useRecords
which have a limited number of fields to create a stateful content variable which I have to pass on to the next react Component. Here’s my code
const { translator, compId } = props;
// Block viewport
const viewport = useViewport();
const [editorVisible, setEditorVisible] = useState(false);
const [content, setContent] = useState(demoContent[0].content);
// Block settings button
useSettingsButton(function () {
if (viewport.isFullscreen) {
setEditorVisible(!editorVisible);
} else {
viewport.enterFullscreenIfPossible();
setEditorVisible(true);
}
});
// read the current tables and translate the content to
// 'content' object
const contentTable = base.getTableByName("Content");
const detailsTable = base.getTableByName("Details");
const contents = useRecords(contentTable);
const details = useRecords(detailsTable);
const relevantDetail = details[0];
const myContent = contents.map((content, index) => {
const contentItem = {};
if (index === 0) {
const fields = Object.keys(translator);
fields.forEach((field) => {
if (field != "Answer" && field != "Emoji Image") {
const key = translator[field];
contentItem[key] = relevantDetail.getCellValueAsString(field);
}
});
}
contentItem[translator["Answer"]] = content.getCellValueAsString("Answer");
contentItem[translator["Emoji Image"]] =
urls[content.getCellValueAsString("Emoji Image")];
return contentItem;
});
setContent(myContent);
// Block fulscreen button
viewport.watch("isFullscreen", function (viewport) {
if (!viewport._isFullscreen && editorVisible) {
setEditorVisible(false);
}
});
// Block HTML template
return (
<EmojiPoll
compId={compId}
isEditorVisible={editorVisible}
content={content}
/>
);
So basically myContent is generated dynamically based on the contents of exactly two tables in the base. These tables have been populated initially, yet I face the following error-
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
at renderWithHooks (https://localhost:9000/__runFrame/bundle.js:62912:17)
at mountIndeterminateComponent (https://localhost:9000/__runFrame/bundle.js:65580:13)
at beginWork (https://localhost:9000/__runFrame/bundle.js:66704:16)
at HTMLUnknownElement.callCallback (https://localhost:9000/__runFrame/bundle.js:48263:14)
What am I doing wrong? Thanks in advance!