亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在反應組件中創建 HTML 標簽

在反應組件中創建 HTML 標簽

慕哥6287543 2022-05-26 14:47:26
我沒有擴展組件類,試圖用來usestate管理狀態?,F在我想在特定條件下將人員組件添加到personList方法內部的變量中togglePersonsHanler。我希望添加一個 HTML 標簽列表,例如<person name="person1" age=31><person name="person2" age=26><person name="person3" age=35>但在控制臺日志上,我得到personList如下{$$typeof: Symbol(react.element), type: "div", key: null, ref: null, props: {…}, …}$$typeof: Symbol(react.element)type: "div"key: nullref: nullprops: {children: Array(3)}_owner: null_store: {validated: false}_self: null_source: {fileName: "D:\data\react\my-app\src\App.js", lineNumber: 72, columnNumber: 7}并且人員標簽沒有被添加到 DOM 中,請提供任何建議import React, { useState } from 'react';import './App.css';import Person from './Person/Person';const App = props => {      const [personState, setPersonState] = useState({    persons: [      {name: "person1", age:31},      {name: "person2", age:26},      {name: "person3", age:25}    ],    other: "some Other Value"  } );  const [otherState,setOtherState]=useState({otherState :'some other value'});  const [showPersonsState,setShowPersonsState]=useState({showPersons :false});    let personList=null;    const togglePersonsHanler =() =>{      personList=null;      setShowPersonsState(        {showPersons : !showPersonsState.showPersons}      )      console.log(showPersonsState.showPersons);      if(showPersonsState.showPersons){        personList=(      <div>{personState.persons.map (person =>{        return <person name={person.name} age={person.age}/>      }      )}</div>        );      }      console.log(personList);    }  return (    <div className="App">      <h1> HI, I'm the react app</h1>      <button       //onClick={switchNameHandler.bind(this,'Gopu Ravi')}       onClick={togglePersonsHanler}      style={style}> Toggle Person </button>      { personList }    </div>  );}export default App;
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

您通過將對象文字用作html 標記來映射它們。您可能打算使用導入的Person組件。


<div>

  {personState.persons.map (person => (

    <Person name={person.name} age={person.age}/>

  )}

</div>

并且要修復一個 react-key 警告,因為所有映射的元素都需要唯一的鍵,添加一個 key prop,其值對于數組中的數據是唯一的,比如 name:


<div>

  {personState.persons.map (person => (

    <Person key={name} name={person.name} age={person.age}/>

  )}

</div>

要正確切換“personList”的顯示:

  1. 如果是,則有條件地渲染映射persons數組showPersonsStatetrue

  2. 將狀態簡化showPersonsState為布爾值

  3. 使用功能狀態更新正確地showPersonsState從以前的狀態切換

更新了組件代碼

const App = props => {

  const [personState, setPersonState] = useState({

    persons: [

      { name: "person1", age: 31 },

      { name: "person2", age: 26 },

      { name: "person3", age: 25 }

    ],

    other: "some Other Value"

  });


  const [otherState, setOtherState] = useState({

    otherState: "some other value"

  });

  const [showPersonsState, setShowPersonsState] = useState(false);


  const togglePersonsHandler = () => setShowPersonsState(show => !show);


  return (

    <div className="App">

      <h1> HI, I'm the react app</h1>

      <button onClick={togglePersonsHandler}>Toggle Person</button>

      {showPersonsState &&

        personState.persons.map(({ age, name }) => (

          <Person key={`${name}`} name={name} age={age} />

        ))}

    </div>

  );

};


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號