第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在反應(yīng)組件中創(chuàng)建 HTML 標(biāo)簽

在反應(yīng)組件中創(chuàng)建 HTML 標(biāo)簽

慕哥6287543 2022-05-26 14:47:26
我沒有擴(kuò)展組件類,試圖用來usestate管理狀態(tài)?,F(xiàn)在我想在特定條件下將人員組件添加到personList方法內(nèi)部的變量中togglePersonsHanler。我希望添加一個(gè) HTML 標(biāo)簽列表,例如<person name="person1" age=31><person name="person2" age=26><person name="person3" age=35>但在控制臺(tái)日志上,我得到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}并且人員標(biāo)簽沒有被添加到 DOM 中,請(qǐng)?zhí)峁┤魏谓ㄗhimport 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貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

您通過將對(duì)象文字用作html 標(biāo)記來映射它們。您可能打算使用導(dǎo)入的Person組件。


<div>

  {personState.persons.map (person => (

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

  )}

</div>

并且要修復(fù)一個(gè) react-key 警告,因?yàn)樗杏成涞脑囟夹枰ㄒ坏逆I,添加一個(gè) key prop,其值對(duì)于數(shù)組中的數(shù)據(jù)是唯一的,比如 name:


<div>

  {personState.persons.map (person => (

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

  )}

</div>

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

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

  2. 將狀態(tài)簡化showPersonsState為布爾值

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

更新了組件代碼

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>

  );

};


查看完整回答
反對(duì) 回復(fù) 2022-05-26
  • 1 回答
  • 0 關(guān)注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)