r/learnreactjs • u/Kayne_Weast • Apr 29 '22
Why isn't my react hook working like you'd expect here? What did I do wrong?
Im trying to pass a state value into a component. Why is it working in one component and not working in another component in the same folder?
I have the hooks in here. Im trying to access "currentGuess". In this function I initialize the state of currentGuess to "", then the next part just sets the "currentGuess" to whatever you type in.
----------------------/src/hooks/useWordle.js----------------------
const useWordle = (solution) => {
const [currentGuess, setCurrentGuess] = useState("");
/* OTHER UNNECESSARY CODE TO QUESTION */
const handleInput = ({ key }) => {
if (key === "Enter") {
if (turn > 5) {
console.log("You used all your guesses!");
return;
}
if (history.includes(currentGuess)) {
console.log("You already tried that word!");
return;
}
if (currentGuess.length !== 5) {
console.log("Word must be 5 characters long!");
return;
}
const formatted = formatGuessWord();
console.log(formatted);
}
if (key === "Backspace") {
setCurrentGuess((state) => {
return state.slice(0, -1);
});
}
if (/^[a-zA-z]$/.test(key))
if (currentGuess.length < 5) {
setCurrentGuess((state) => {
return state + key;
});
}
};
return {
currentGuess,
handleInput,
};
};
export default useWordle;I can use it in here like this and it works no problem:
----------------------src/components/Wordle.js----------------------
import React, { useEffect } from "react";
import useWordle from "../hooks/wordleHooks.js";
function Wordle({ solution }) {
const { currentGuess, handleInput } = useWordle(solution);
console.log("currentGuess=", currentGuess);
useEffect(() => {
window.addEventListener("keyup", handleInput);
return () => window.removeEventListener("keyup", handleInput);
});
return <div>Current guess: {currentGuess}</div>;
}
export default Wordle;I thought this line was what allowed me to use "currentGuess". I destructured it.
const { currentGuess, handleInput } = useWordle(solution);
However when I place that line in this code, "currentGuess" comes out undefined or empty.
----------------------/src/components/Key.js----------------------
import React, { useContext } from "react";
import { AppContext } from "../App";
import useWordle from "../hooks/wordleHooks.js";
export default function Key({ keyVal, largeKey }) {
const { onSelectLetter, onDeleteKeyPress, onEnterKeyPress } =
useContext(AppContext);
const { currentGuess } = useWordle();
const handleTypingInput = () => {
console.log("Key.js - Key() - handleTypingInput() - {currentGuess}= ", {
currentGuess,
}); // COMES OUT "Object { currentGuess: "" }"
};I tried to make this as easy to read as possible, they wont let you use spoiler tags to hide the code and make it easier to read at first, so if you made it this far thank you very much.
Im new to this and hoping someone who knows what they are doing can see some glaring flaw I can fix. You don't even have to solve it for me but can you point me in the right direction? How do I get the "currentGuess" in the Key.js component?
1
u/Most_Original_Name Apr 29 '22
I’m looking at this on mobile, but at first glance it seems you forgot to pass the ‘solution’ parameter to useWordle() ?
1
u/link3333 Apr 29 '22 edited Apr 29 '22
Forgive me if I am misunderstanding the problem, but it seems like a component state issue, where the state is not shared between different components. This is normal for React.
So I think React keeps some internal tracking of hook calls for each instance of a component. When you render a Key, it calls useWordle, which calls useState. First time it's rendering the component, so it sets up the initial state value for the first hook call for this instance of Key. Next time Key renders, it refers to the hook calls performed on the last render and can pick up the state for that Key instance.
If you are rendering a Wordle component next to the Key component, the first time it renders the Wordle, it would encounter the useState hook and keep track of it for the Worldle component instance. If you had another Wordle component, it would be keeping track of its own useState hook.
So 3 component instances and 3 different states, all defaulting to an empty string. Note this would be the same problem if you didn't use hooks or functional components (using class components).
So you probably want a single piece of state that multiple components can share. For this simple case I'd have the parent component of Worldle and Key to actually call the useWordle custom hook so that there's just one component instance storing the currentGuess state. Wordle and Key would need to accept props for the currentGuess and/or handleInput.
An alternative for larger projects would be something like react-redux where any component can have access to retrieve and update application-level state (and the state is managed by redux code).
Here's a React docs page about moving state up the hierarchy. https://reactjs.org/docs/lifting-state-up.html The example uses class components instead of functional components with hooks.