变量(‘person’)未定义?(基本的映射方法,React JS)

huangapple go评论66阅读模式
英文:

A variable('person') is not defined ? (basic map method, React JS)

问题

I want to render the information contained in an object. So I use the map method like this,

<ul>
    {persons.map(person => <li key={person.id}> person.name </li>)}
</ul>

In VS code and console on the browser, there were no error. But in the main screen it displays 'person' is not defined. As far as I know, I am executing the map method correctly. How can I fix this? Here are the rest of the code. How can

App.js

import { useState } from 'react'

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

  const [newName, setNewName] = useState('')

  const addName = (event) => {
    event.preventDefault()

    const nameObject = {
      name: newName,
      id: persons.length + 1
    }

    setPersons(persons.concat(nameObject))
    setNewName('')
  }
  console.log(persons)

  const handleNameChange = (event) => {
    setNewName(event.target.value)
  }

  return (
    <div>
      <h2>Phonebook</h2>

      <form onSubmit={addName}>
        <div>
          name: <input value={newName} onChange={handleNameChange} />
        </div>
        <div>
          <button type="submit">add</button>
        </div>
      </form>

      <h2>Numbers</h2>

      <ul> 
        {persons.map(person => <li key={person.id}>{person.name}</li>)}
      </ul>
      
    </div>
  )
}

export default App

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')).render(<App />)
英文:

I want to render the information contained in an object. So I use the map method like this,

&lt;ul&gt; 
    {persons.map(person =&gt; &lt;li key={person.id}&gt; person.name &lt;/li&gt;)}
&lt;/ul&gt;

In VS code and console on the browser, there were no error. But in the main screen it display 'person' is not defined. As far as I know, I am executing the map method correctly. How can I fix this ? Here are the rest of the code. How can

App.js

    import { useState } from &#39;react&#39;
    
    
    const App = () =&gt; {
      const [persons, setPersons] = useState([
        { 
          name: &#39;Arto Hellas&#39;,
          id: 1 
        }
      ])
    
      const [newName, setNewName] = useState(&#39;&#39;)
    
      const addName = (event) =&gt; {
        event.preventDefault()
    
        const nameObject = {
          name: newName,
          id: persons.length + 1
        }
    
        setPersons(persons.concat(nameObject))
        setNewName(&#39;&#39;)
      }
      console.log(persons)
    
      const handleNameChange = (event) =&gt; {
        setNewName(event.target.value)
      }
    
      return (
        &lt;div&gt;
          &lt;h2&gt; Phonebook &lt;/h2&gt;
    
          &lt;form onSubmit={addName}&gt;
            &lt;div&gt;
              name: &lt;input value={newName} onChange={handleNameChange} /&gt;
            &lt;/div&gt;
            &lt;div&gt;
              &lt;button type=&quot;submit&quot;&gt; add &lt;/button&gt;
            &lt;/div&gt;
          &lt;/form&gt;
    
          &lt;h2&gt; Numbers &lt;/h2&gt;
    
          &lt;ul&gt; 
            {persons.map(person =&gt; &lt;li key={person.id}&gt; person.name &lt;/li&gt;)}
          &lt;/ul&gt;
          
    
        &lt;/div&gt;
      )
    }

export default App

index.js

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import App from &#39;./App&#39;;

ReactDOM.createRoot(document.getElementById(&#39;root&#39;)).render(&lt;App /&gt;)

答案1

得分: 3

需要将 person.name 更改为 {person.name}

英文:

You need to change person.name to {person.name}

答案2

得分: 3

使用&lt;li/&gt;内部的错误

&lt;li&gt; key={person.id}&gt; {person.name} &lt;/li&gt;

它有效。我在错误中没有看到'变量('person')未定义'。

https://codesandbox.io/s/inspiring-thompson-7f6hqj?file=/src/App.js

英文:

Mistake with using persone inside &lt;li/&gt;

&lt;li&gt; key={person.id}&gt; {person.name} &lt;/li&gt;

And its works. I didint see 'A variable('person') is not defined' in errors

https://codesandbox.io/s/inspiring-thompson-7f6hqj?file=/src/App.js

答案3

得分: 3

你在代码的一些细节上遗漏了。

    <ul>
      {persons.map((person) => {
        return <li key={person.id}> {person.name} </li>;
      })}
    </ul>;
英文:

You're missing on minor code details.

&lt;ul&gt;
  {persons.map((person) =&gt; {
    return &lt;li key={person.id}&gt; {person.name} &lt;/li&gt;;
  })}
&lt;/ul&gt;;

答案4

得分: 2

在一个 JSX 元素内,将组件的变量放在花括号 "{}" 中。

英文:

Remember to put inside a JSX element the variables of the component between "{}".

huangapple
  • 本文由 发表于 2023年2月16日 17:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定