'How to apply data into useState constant?
I was trying to set a value to a input, and I usead setState to update the value of the Input, but now I cant use the variable I was going to use in the Value inside Input.
This is the Input
<input
type="text"
className="mt-1 focus:ring-indigo-500 focus:border-[#604d9b] block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"
value={name}
placeholder="Type any name you want"
onChange={(e) => setName(e.target.value)}
/>
and this is my hook.
const [name, setName] = useState('');
Now, I want to apply this variable but I cant do it directly to the constant inside the useState
userData[0]?.name;
Anyone knows how to apply my
userData[0]?.name;
Into
const [name, setName] = useState('');
Solution 1:[1]
It sounds like you're trying to set the initial value for your name state.
If this is the case, you can simply pass it as an argument to useState:
const [name, setName] = useState(userData[0]?.name || '');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Corey Larson |
