Definition of State State is internal mutable data inside a component. It determines how a component looks and behaves. When state changes, React re-renders the component.
Declaring State Functional Components: useState hook. Class Components: this.state and this.setState(). Preferred: Functional with Hooks.
Updating State Never mutate directly. Always use setter function. Use functional updates if new state depends on previous value.
Multiple State Values Use multiple useState calls for separate values. Or use a single object for related values. Always copy state objects when updating.
State vs Props Props: Passed from parent, immutable. State: Managed within component, mutable. Props configure, State manages behavior.
Events and State User interactions update state. Example: toggle button, form input, counter.
Derived State Don’t duplicate data in state if it can be derived. Keep state minimal and consistent.
Lifting State Up When siblings need same state, lift it up to their parent. Parent manages state, passes down as props.
State Lifecycle Initialization → Update → Re-render → Cleanup. Cleanup often handled with useEffect.
Best Practices Keep state minimal. Use functional updates. Avoid deep nesting. Reset when needed. Use Context/Redux for global state.
Complex State Structures Use arrays/objects with immutability. Flatten nested states if possible. Update safely with spread operator.
Prop Drilling Passing props through many layers. Becomes difficult in deep component trees. Solution: Context API.
Context API Share state across many components without drilling. Create context, provide at top, consume with useContext.
Sibling Communication Siblings communicate via parent’s lifted state. Parent acts as mediator.