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

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

嘗試將數組轉換為列表

嘗試將數組轉換為列表

小怪獸愛吃肉 2023-06-29 22:39:40
我的函數存在問題,該函數將數據從數組轉換為不同組件中的列表。我認為問題在于我不了解在哪里放置document.GetElementById(). 會出現錯誤document.getElementById(...) is null。是因為我嘗試在渲染之前訪問特定位置嗎?那我應該如何訪問它,也許它與組件生命周期有關?這是我的代碼:import React, { Component } from 'react';import Day from "./day";import image1 from "./img/eggs.jpg";class Stuff extends Component {  constructor(props){    super(props);    this.makeList = this.makeList.bind(this);  }     makeList(array) {      var list = document.createElement('ul');      for (var i = 0; i < array.length; i++) {          var item = document.createElement('li');          item.appendChild(document.createTextNode(array[i]));          list.appendChild(item);      }      return list;  }  render() {    const source =  {        breakfast: [          {            id: 1,            name: "eggs",            img: image1,            description: "Start a day with delicious and nutricious eggs!",            ingridients: ['2 eggs', 'two slices of toast', 'bacon', 'butter']          },          ...        ]}    return (      <div>        <Day {...source}             makeList={this.makeList} />      </div>    );  }}export default Stuff;和 Day 組件,React 發生錯誤:import React, { Component } from 'react';import "./day.css";class Day extends Component {  render() {    const appChild = document.getElementById('foo').appendChild(this.props.makeList(this.props.source.breakfast.ingridients));    return (        <div className="displayOne">          <img src= {this.props.breakfast[0].img} alt="eggs" />          <h3>{this.props.breakfast[0].description}</h3>          <div id="foo">          <p>{appChild}</p>          </div>        </div>    );  }}export default Day;感謝您的幫助和理解!
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

你可能應該使用 jsx 而不是直接操作 dom:


function makeList(array) {

? ? return (

? ? ? ? <ul>

? ? ? ? ? ? (array.map((value, index) => (<li>{value}</li>)

? ? ? ? </ul>

? ? )

}

或者一個完整的、更優化的解決方案是創建一個Breakfast組件:


class Stuff extends Component {

? ? constructor(props) {

? ? ? ? super(props);


? ? ? ? this.source = {

? ? ? ? ? ? breakfast: [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? id: 1,

? ? ? ? ? ? ? ? ? ? name: "eggs",

? ? ? ? ? ? ? ? ? ? img: image1,

? ? ? ? ? ? ? ? ? ? description: "Start a day with delicious and nutricious eggs!",

? ? ? ? ? ? ? ? ? ? ingridients: ['2 eggs', 'two slices of toast', 'bacon', 'butter']

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ]

? ? ? ? }


? ? }


? ? render() {

? ? ? ? return (

? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? <Day source={this.source}></Day>

? ? ? ? ? ? </div>

? ? ? ? );

? ? }

}


class Day extends Component {

? ? render() {

? ? ? ? return (

? ? ? ? ? ? <div className="displayOne">

? ? ? ? ? ? ? ? {this.props.source.breakfast.map((breakfast) => <Breakfast breakfast={breakfast}/>)}

? ? ? ? ? ? </div>

? ? ? ? );

? ? }

}


function Breakfast({breakfast}) {

? ? return (

? ? ? ? <div className="displayOne">

? ? ? ? ? ? <img src={breakfast.img} alt="eggs"/>

? ? ? ? ? ? <h3>{breakfast.description}</h3>

? ? ? ? ? ? <ul>

? ? ? ? ? ? ? ? {breakfast.ingridients.map((ingridient) => <li>{ingridient}</li>)}

? ? ? ? ? ? </ul>

? ? ? ? </div>

? ? )

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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