Skip to content

Commit a4db3ce

Browse files
authored
Use React.useRef for blocklyComponent, instead of React.useState. (#327)
1 parent 7eaa861 commit a4db3ce

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/reactComponents/TabContent.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const TabContent = React.forwardRef<TabContentRef, TabContentProps>(({
7474
setAlertErrorMessage,
7575
isActive,
7676
}, ref) => {
77-
const [blocklyComponent, setBlocklyComponent] = React.useState<BlocklyComponentType | null>(null);
77+
const blocklyComponent = React.useRef<BlocklyComponentType | null>(null);
7878
const [editorInstance, setEditorInstance] = React.useState<editor.Editor | null>(null);
7979
const [generatedCode, setGeneratedCode] = React.useState<string>('');
8080
const [triggerPythonRegeneration, setTriggerPythonRegeneration] = React.useState(0);
@@ -116,15 +116,16 @@ export const TabContent = React.forwardRef<TabContentRef, TabContentProps>(({
116116
}
117117

118118
// Check if this event is for our workspace
119-
if (blocklyComponent && event.workspaceId === blocklyComponent.getBlocklyWorkspace().id) {
119+
if (blocklyComponent.current &&
120+
event.workspaceId === blocklyComponent.current.getBlocklyWorkspace().id) {
120121
setTriggerPythonRegeneration(Date.now());
121122
// Also notify parent
122123
}
123124
}, [blocklyComponent]);
124125

125126
/** Called when BlocklyComponent is created. */
126127
const setupBlocklyComponent = React.useCallback((_modulePath: string, newBlocklyComponent: BlocklyComponentType) => {
127-
setBlocklyComponent(newBlocklyComponent);
128+
blocklyComponent.current = newBlocklyComponent;
128129
newBlocklyComponent.setActive(isActive);
129130
}, [isActive]);
130131

@@ -156,8 +157,8 @@ export const TabContent = React.forwardRef<TabContentRef, TabContentProps>(({
156157

157158
/** Update active state when isActive changes. */
158159
React.useEffect(() => {
159-
if (blocklyComponent) {
160-
blocklyComponent.setActive(isActive);
160+
if (blocklyComponent.current) {
161+
blocklyComponent.current.setActive(isActive);
161162
}
162163
if (editorInstance && isActive) {
163164
editorInstance.makeCurrent(project, modulePathToContentText);
@@ -166,9 +167,9 @@ export const TabContent = React.forwardRef<TabContentRef, TabContentProps>(({
166167

167168
/** Generate code when regeneration is triggered. */
168169
React.useEffect(() => {
169-
if (blocklyComponent && module) {
170+
if (blocklyComponent.current && module) {
170171
const code = extendedPythonGenerator.mrcWorkspaceToCode(
171-
blocklyComponent.getBlocklyWorkspace(),
172+
blocklyComponent.current.getBlocklyWorkspace(),
172173
module
173174
);
174175
setGeneratedCode(code);

0 commit comments

Comments
 (0)