import {useUnit} from 'effector-react';
import React, {useEffect, useRef} from 'react';
import {SymbolInput} from '@/features/input-word/ui/symbol-input/index.jsx';
import * as styles from './word-input.module.css';
import {$userGuess} from '@/features/input-word/model/store.js';
import {checkUserGuess, initUserGuess} from "@/features/input-word/model/events.js";
return (
{ userGuess.map(({idx, value, isRevealed}) =>
React.memo(<SymbolInput
key={${value}-${idx}}
ref={el => inputRefs.current[idx] = el}
idx={idx}
value={value}
isRevealed={isRevealed}
focusNext={focusNextSymbol}
focusPrev={focusPrevSymbol}
/>)
)}
);
}; Objects are not valid as a React child (found: object with keys {$$typeof, type, compare}). If you meant to render a collection of children, use an array instead.
at throwOnInvalidObjectType (http://localhost:3000/bundle.js:19774:9)
at createChild (http://localhost:3000/bundle.js:20026:7)
at reconcileChildrenArray (http://localhost:3000/bundle.js:20291:25)
at reconcileChildFibers (http://localhost:3000/bundle.js:20708:16)
at reconcileChildren (http://localhost:3000/bundle.js:25839:28)
at updateHostComponent ()
at beginWork ()
at HTMLUnknownElement.callCallback ()
at Object.invokeGuardedCallbackDev ()
at invokeGuardedCallback () после долбавления react memo
Objects are not valid as a React child (found: object with keys {$$typeof, type, compare}). If you meant to render a collection of children, use an array instead.
occurs because of incorrect usage of React.memo. Let's break down why this error happens and how to fix it.
The key issue here is that React.memo is being called inside the map function and is wrapping a component instance (the result of <SymbolInput />), rather than the component definition.
React.memo is a higher-order component (HOC) that takes a component type (the component function or class), and returns a new memoized component. It should not be used to wrap the result of rendering (the JSX element).
When you wrap an element instance with React.memo, it returns a new component type (an object with { $$typeof, type, compare }), which React then tries to render as a child. Since React expects elements (JSX) or primitive values as children, it throws an error when it encounters an object representing a component type.
How to Fix It
To fix the error, you need to apply React.memo correctly by wrapping the component definition, not the component instance. Here are two ways to do it:
Option 1: Memoize the Component at Definition
Modify your SymbolInput component to be memoized when it's defined:
Do Not Wrap JSX Elements with React.memo: Wrapping <SymbolInput /> with React.memo returns a new component type, not an element to render. React expects elements (like <div>, <SymbolInput />) or primitive types (strings, numbers) as children, not component types.
Memoize Component Definitions: React.memo should wrap the component function or class, enhancing it to add memoization. This means React will skip re-rendering the component if its props haven't changed, improving performance.
Use the Memoized Component Normally: After memoizing the component, you can use it just like any other component in your JSX.
Additional Tips
Avoid Memoizing Inside Render: Defining MemoizedSymbolInput inside the WordInput component is acceptable but ensures it's outside of the render cycle to prevent unnecessary re-definition. Alternatively, define it outside or at the module level.
Memoization Strategies: Use memoization when you have performance issues due to unnecessary re-renders. Ensure that your components are pure and that memoization will have the desired effect.
Conclusion
By correctly applying React.memo to the component definition, you resolve the error and ensure that your SymbolInput components are properly memoized. This allows React to efficiently manage re-renders, potentially improving the performance of your application.