iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
useState creates state for a component; it does not create a component. If a component function is declared inside another component, every parent render creates a new function identity. React can treat that nested function as a different component, replace its subtree, and reset local state. Move the child function to module scope, render it with JSX such as <Child />, and lift state to the parent only when the parent truly needs to control it.
What this symptom usually means
The wording “creating a component through useState used in another useState created component” mixes two separate ideas. A call to useState stores data and gives you a setter that schedules a render. It does not manufacture a React component, and it cannot be used as a replacement for a component declaration.
A normal React tree can contain a parent and child. The problem is usually the location of the child function declaration: the parent defines Child inside its own function body, then returns <Child />. When any state in the parent changes, the parent function runs again and creates a new Child function. React sees a different component type in that position and may discard the old child subtree, including its input text, focus, effects, and other local state. React documents this as a pitfall in Preserving and Resetting State.
Typical symptoms include an input that becomes empty whenever a counter is clicked, a text area that loses focus on every parent update, an effect that appears to mount repeatedly, or a child form whose state vanishes after an unrelated parent change. Those symptoms are not evidence that useState failed; they indicate that React was given a new component identity.
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
How React decides whether to preserve state
React associates state with a component’s position in the rendered tree. The position includes the component type and, when lists are involved, its key. If the same type remains at the same position, React can preserve its state. If the type is replaced, or its key changes, React creates a fresh subtree and initializes state again. The explanation in State: A Component’s Memory describes this relationship between state and a component’s place in the tree.
The nested-definition trap
Consider this pattern:
import { useState } from 'react';
export default function Parent() {
const [count, setCount] = useState(0);
function Child() {
const [text, setText] = useState('');
return (
<input
value={text}
onChange={event => setText(event.target.value)}
/>
);
}
return (
<>
<Child />
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
</>
);
}
The first render creates one function object for Child. Clicking the button calls setCount, which renders Parent again. During that second call, JavaScript creates another function object for Child. Although both functions have the same name and source text, they are not the same function value. React can therefore replace the old child instead of updating it, so the child’s text state starts over.
The issue is not that the child is rendered inside the parent. Rendering children inside parents is the normal composition model. The issue is defining the component function inside the parent’s render function. React’s documentation summarizes the rule as: do not nest component definitions, or you can reset state by accident.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsJSX is not the same as calling a component
Use JSX to let React own the component lifecycle:
<Child />
Do not invoke a component function as an ordinary JavaScript function:
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Child()
With JSX, React establishes the component boundary, tracks its hooks, and reconciles its returned tree. A direct call runs the function as part of the caller’s execution. Hooks inside that function then belong to the caller’s render, which makes the code harder to reason about and can violate the Rules of Hooks when the call is conditional or moved into a loop. The useState reference and React’s hook rules require hooks to be called at the top level of the component or custom Hook that owns them.
Move the child component to module scope
Define the child outside the parent, usually at the top level of the file or in its own file. Its function identity then remains stable between parent renders.
import { useState } from 'react';
function Child() {
const [text, setText] = useState('');
return (
<input
value={text}
onChange={event => setText(event.target.value)}
placeholder='Type here'
/>
);
}
export default function Parent() {
const [count, setCount] = useState(0);
return (
<>
<Child />
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
</>
);
}
Now a click still rerenders Parent, but the element at the child’s position has the same component type. React updates the existing child and preserves its input state. The child and parent each own the state that belongs to their job: Parent owns the counter, while Child owns the draft text.
Putting the child in another file
For a reusable component, export it from a separate module and import it where needed:
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
// Child.jsx
import { useState } from 'react';
export default function Child() {
const [text, setText] = useState('');
return (
<input
value={text}
onChange={event => setText(event.target.value)}
/>
);
}
// Parent.jsx
import { useState } from 'react';
import Child from './Child.jsx';
export default function Parent() {
const [count, setCount] = useState(0);
return (
<section>
<Child />
<button onClick={() => setCount(current => current + 1)}>
{count}
</button>
</section>
);
}
Module scope is the important part, not whether the code is in one file or two. A top-level declaration can still receive props, use context, and contain its own hooks.
Choose where the state should live
Keeping state in the child is correct when the value is private to that child. A search field, disclosure toggle, or draft note often belongs there. The parent can rerender for unrelated reasons without changing the child’s value.
Lift state when two components need the same source of truth, or when the parent must validate, submit, reset, or coordinate the value. React’s recommended pattern is described in Sharing State Between Components: move the state to the closest common parent and pass the current value plus event handlers as props.
Free tools Windows power users keep installed
One-click scans. No signup required.
Controlled child example
import { useState } from 'react';
function ChildInput({ value, onChange }) {
return (
<label>
Name
<input value={value} onChange={event => onChange(event.target.value)} />
</label>
);
}
export default function Parent() {
const [name, setName] = useState('');
const [count, setCount] = useState(0);
return (
<>
<ChildInput value={name} onChange={setName} />
<p>Preview: {name || 'Nothing entered'}</p>
<button onClick={() => setCount(current => current + 1)}>
Render count: {count}
</button>
</>
);
}
Here the child does not create a second copy of the name. It displays the value supplied by the parent and reports edits through onChange. This prevents the two components from drifting apart. Do not lift state merely because a parent happens to rerender; lift it when ownership or coordination requires it.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Rules for useState that affect this bug
Call hooks at the top level
Call useState unconditionally at the top level of the component or custom Hook that owns the state. Do not put it inside an if statement, loop, event handler, nested callback, or component that you invoke manually. React relies on the stable order of hook calls from one render to the next. The API reference at react.dev/reference/react/useState covers these constraints.
// Correct
function Panel() {
const [open, setOpen] = useState(false);
if (!open) return <button onClick={() => setOpen(true)}>Open</button>;
return <div>Content</div>;
}
// Incorrect: the hook order changes when condition changes
function BrokenPanel({ enabled }) {
if (enabled) {
const [open, setOpen] = useState(false);
return <button onClick={() => setOpen(true)}>Open</button>;
}
return null;
}
The initial value is not reapplied on every render
The argument passed to useState initializes state only when that component instance is created. Later renders ignore that argument. If a prop changes and you want to synchronize or replace state, decide explicitly whether the value should be controlled by the parent, updated in an effect, or reset by a key. Simply changing useState(initialValue) does not reset an existing instance.
Use the previous value when updates depend on it
A setter schedules a render; it does not immediately replace the current variable in the already-running function. When the next value depends on the previous one, use the updater form:
setCount(current => current + 1);
This is especially useful when several updates may be queued. It fixes stale-value calculations, but it does not fix a nested component definition. Component identity and state-update timing are separate concerns.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Reset a child deliberately with a key
Sometimes losing state is the desired behavior. For example, an editor should start blank when switching from one customer record to another. Give the child a key based on that identity:
function Editor({ recordId }) {
const [draft, setDraft] = useState('');
return <textarea value={draft} onChange={event => setDraft(event.target.value)} />;
}
function Screen({ recordId }) {
return <Editor key={recordId} recordId={recordId} />;
}
When recordId changes, React intentionally creates a new Editor subtree, so draft returns to its initial value. Use a key only for an intentional identity boundary. An unstable key such as an array index in a reorderable list, a random value, or a value that changes on every render causes needless remounts and the same state-loss symptoms.
A practical debugging checklist
- Find component declarations inside component bodies. Move every nested function that returns JSX to module scope. Nested helper functions that do not render JSX are not automatically a problem, but a component declaration is.
- Search for direct component calls. Replace
Child()with<Child />. Pass data as props rather than positional function arguments. - Check hook placement. Ensure each
useStatecall is at the top level of a stable component or custom Hook, never in a branch, loop, callback, or event handler. - Inspect keys. Verify that the child’s key is stable unless a reset is explicitly intended. In lists, use a data identifier that remains attached to the same item.
- Identify the actual owner. If the parent must read or reset the value, lift that state and pass
valueand callbacks. Otherwise let the child keep private state. - Separate initialization from synchronization. A changed prop does not re-run the initial state expression. Choose controlled props, an explicit update, or a key based on the desired behavior.
- Test the smallest reproduction. Type into the child, trigger an unrelated parent update, and observe whether the value remains. If it disappears, log the child’s mount and unmount or temporarily remove keys to locate the identity change.
What the fixed behavior should look like
After moving the child definition to module scope, typing in its input and clicking the parent’s button should leave the typed text unchanged. The parent counter should update independently, and the child should rerender only as needed without being recreated as a new component type. If the text still resets, inspect the child’s key, any conditional that swaps it for a different component, and whether a surrounding list is being reordered.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteThe durable model is simple: state belongs to a component instance, and an instance is identified by its type, position, and key. Keep component definitions stable, render them through JSX, obey hook ordering, and lift state only when shared ownership demands it. Those rules address the failure described here without adding a second state copy or forcing an unnecessary remount.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

