Course contentsShow
← React
Lesson 355 of 2,39610. useState BasicsFree lesson

Complex Initial State Values

Initialize with objects, arrays, or any JavaScript value as initial state.

Complex Initial State Values

What you'll learn: How to initialize useState with objects, arrays, or any JavaScript value—not just simple strings and numbers.

State Can Hold Any JavaScript Value

So far, you've likely initialized state with primitives like 0, "", or false. But useState doesn't limit you—your initial state can be any valid JavaScript value: objects, arrays, even functions (though we'll save that special case for later).

// Simple values work
const [count, setCount] = useState(0);

// But so do complex structures
const [user, setUser] = useState({
  name: "Alice",
  age: 30,
  isLoggedIn: true
});

const [items, setItems] = useState(["apple", "banana", "cherry"]);

const [profile, setProfile] = useState({
  info: { firstName: "Bob", lastName: "Smith" },
  preferences: ["dark-mode", "notifications"],
  settings: { volume: 75 }
});

Why This Matters

Real applications often need structured data. Instead of managing five separate state variables for a form, you can use one object. Instead of tracking unrelated counters, you can keep an array of tasks.

// Instead of this:
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [email, setEmail] = useState("");

// You can do this:
const [form, setForm] = useState({
  firstName: "",
  lastName: "",
  email: ""
});

The Same Rules Apply

No matter how complex your initial value, remember: this value is only used once—when the component first renders. After that, React preserves whatever state you've set through setForm or your setter function.

Key Takeaway: You can initialize useState with objects, arrays, or any JavaScript structure—state isn't limited to simple values, making it perfect for real-world data.