React Error and console.log Issue


If I console.log(persons), it shows up for a fraction of a second and then disappears (it originally flashed an object, but now it flashes uncaught). If I type persons in the console it gives this error: VM675:1 Uncaught ReferenceError: persons is not defined at :1:1.

Anyone know what’s causing the error and why the console.log isn’t staying on the screen? Thanks

import { useState } from 'react'

const App = () => {
  const [persons, setPersons] = useState([
    { name: 'Arto Hellas' }
  const [newName, setNewName] = useState('')

  function handleSubmit() {
    const personObj = {
      name: newName

  function handleNameChange(e) {

  return (
      <div>debug: {newName}</div>
      <form onSubmit={handleSubmit}>
          name: <input onChange={handleNameChange} type="text" value={newName} />
          <button type="submit">add</button>
          { => {

export default App

Ok, maybe it’s just something on my end. I’m tring to get the names to print below ‘Numbers’. Can you give me a hint as to where I’m going wrong? Heres where I’m at so far. Thanks

Got it working. I needed parentheses instead of curly braces, if anyone’s interested:


          { => {   <-- need () instead of {}
            <PersonItem key={} person={person} />


          { => (
            <PersonItem key={} person={person} />

Did you fix it? It seems to work.

As an aside, for logging state variables using a useEffect is a good way of doing it.

useEffect(() => {
}, [someState]);
1 Like

You can do both, but you have to remember the return if you use a function body.

1 Like

Ya, got it working, but now it’s giving me: ‘Warning: Each child in a list should have a unique “key” prop.’ I guess I’m doing that wrong? And thanks for the tip about useEffect. That’ll come in handy!

The initial state object doesn’t have an id property.

Also, what the user types can not be guaranteed to be unique. Use a random value or something you know is unique for each object.

You should be able to use the global randomUUID method (do it in the handler, not the loop).


You should not declare components inside components. Move the PersonItem component definition out of the App component.

1 Like

Thanks, that’s what was causing the error.

Ya, I know, I was wondering about that too. I’m just following a tutorial. I’m sure they’ll go into that further a long. They just said to just use the names for now. In the next exercise, we’re supposed to: “Prevent the user from being able to add names that already exist in the phonebook.”

I used nanoid once before. Haven’t used UUID yet, but I’ll try it if they don’t go into it before the end of this project.

Thanks for pointing that out. Just read something about that yesterday. Totally forgot.

Thanks for all the feedback!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.