It seems you're experiencing unwanted re-renders of your WordInput component every time a user inputs a character. Your goal is to prevent the WordInput component from re-rendering on each input and only have the specific SymbolInput components re-render when necessary.
Understanding the Issue
The main reason your WordInput component is re-rendering is because it's subscribed to the $userGuess store via useUnit($userGuess). Every time the store updates (which happens on each character input), useUnit triggers a re-render of the component.
Here's the line that's causing the re-render:
const userGuess = useUnit($userGuess);
By using useUnit($userGuess), you're telling React to re-render WordInput whenever $userGuess changes.
Solution
To prevent WordInput from re-rendering on each input, you need to remove its direct dependency on the $userGuess store. Instead, you can:
- Avoid subscribing to
$userGuess in WordInput.
- Render a fixed number of
SymbolInput components based on the length of the word.
- Let each
SymbolInput component manage its own subscription to the store.
Here's how you can adjust your code:
Step 1: Modify WordInput to Avoid Subscribing to $userGuess
Remove the line where you use useUnit($userGuess):
-export const WordInput = ({}) => {
- const userGuess = useUnit($userGuess);
Instead, you can get the word length once, perhaps from an initial prop or a constant value, depending on your application's logic.
Assuming you have a way to determine the word length (e.g., from $word store or a prop), you can use useUnit($userGuessLength) just once to get the length, since the length doesn't change during the game:
import { useUnit } from 'effector-react';
export const WordInput = () => {
const wordLength = useUnit($userGuessLength); // Assuming the length doesn't change
const inputRefs = useRef([]);
// Rest of your logic
Step 2: Render a Fixed Number of SymbolInput Components
Now, instead of mapping over userGuess, you can render SymbolInput components based on the word length:
return (
<div className={styles.wordDisplay}>
{Array.from({ length: wordLength }).map((_, idx) => (
<SymbolInput
key={idx}
ref={(el) => (inputRefs.current[idx] = el)}
idx={idx}
focusNext={focusNextSymbol}
focusPrev={focusPrevSymbol}
/>
))}
</div>
);
Step 3: Manage Store Subscription Inside SymbolInput
Your SymbolInput component is already independently subscribing to the $userGuess store using useStoreMap. This means each SymbolInput re-renders only when its corresponding part of the store changes:
const { value, isRevealed } = useStoreMap({
store: $userGuess,
keys: [idx],
fn: (symbols, [idx]) => symbols[idx],
});
Complete WordInput Component Example
Here's how your WordInput component might look after these changes:
import { useUnit } from 'effector-react';
import React, { useEffect, useRef, useCallback } from 'react';
import { SymbolInput } from '@/features/input-word/ui/symbol-input/index.jsx';
import * as styles from './word-input.module.css';
import { checkUserGuess } from '@/features/input-word/model/events.js';
export const WordInput = () => {
const wordLength = useUnit($userGuessLength); // Get the length once
const inputRefs = useRef([]);
const focusNextUnopenedSymbol = useCallback(
(currentIdx, step) => {
// Your existing focus logic
},
[wordLength] // Dependency array without userGuess
);
const focusNextSymbol = useCallback(
(currentIdx) => {
focusNextUnopenedSymbol(currentIdx, 1);
},
[focusNextUnopenedSymbol]
);
const focusPrevSymbol = useCallback(
(currentIdx) => {
focusNextUnopenedSymbol(currentIdx, -1);
},
[focusNextUnopenedSymbol]
);
useEffect(() => {
const handleEnter = (event) => {
if (event.key !== 'Enter') return;
checkUserGuess();
};
document.addEventListener('keydown', handleEnter);
return () => document.removeEventListener('keydown', handleEnter);
}, []);
return (
<div className={styles.wordDisplay}>
{Array.from({ length: wordLength }).map((_, idx) => (
<SymbolInput
key={idx}
ref={(el) => (inputRefs.current[idx] = el)}
idx={idx}
focusNext={focusNextSymbol}
focusPrev={focusPrevSymbol}
/>
))}
</div>
);
};
Explanation
By removing the direct subscription to $userGuess in WordInput, the component will no longer re-render when $userGuess updates. Instead, each SymbolInput component will individually subscribe to the specific part of $userGuess that it cares about.
-
Pros:
WordInput doesn't re-render unnecessarily.
- Each
SymbolInput re-renders only when its own data changes.
- Improved performance and avoids unnecessary renders.
-
Cons:
- If
wordLength changes dynamically during the game (e.g., the word length can change), you'll need to ensure you handle that appropriately, possibly by including wordLength in the dependency array of your useEffects or by managing it via state.
Additional Recommendations
-
Use React.memo for WordInput (Optional):
If you find that WordInput is still re-rendering due to other props or state changes, you can wrap it with React.memo:
export const WordInput = React.memo(() => {
// Component code
});
However, in this case, since we've removed the direct subscription to the store, wrapping WordInput with React.memo might not be necessary unless there are other props causing re-renders.
-
Check Other Dependencies:
Ensure that other variables used in useCallback or useEffect hooks don't cause unnecessary re-renders. For example, if you include variables in dependency arrays that change frequently, they can trigger re-renders or re-creations of functions.
Conclusion
By restructuring your components to limit subscriptions to where they're absolutely necessary, you can prevent unwanted re-renders and improve the performance of your application. Moving the subscription to the individual SymbolInputs allows them to update independently without affecting the parent WordInput component.