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

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

道具在reactjs中沒有正確的數(shù)據(jù)

道具在reactjs中沒有正確的數(shù)據(jù)

慕的地6264312 2023-09-07 16:08:44
我使用 firebase 數(shù)據(jù)庫,并將該數(shù)據(jù)庫中的數(shù)據(jù)轉(zhuǎn)換為 JSON 格式。有了這些數(shù)據(jù),我正在使用地圖函數(shù),我想將我的數(shù)據(jù)渲染到其他組件中。我的代碼如下所示。第一個(gè)組件function Products() { const [url, setUrl] = useState([]);useEffect(() => {    async function asyncCall() {        const myurl = await axios.get("i put a example link here:mydata.json")            setUrl(myurl.data)    }    asyncCall();},[]);return (    <Row>        {url.map((url => (                <Col key={url.id} sm={12} md={6} lg={4} xl={3}>                    <Bags url={url} />                </Col>                    )        ))}    </Row>)}我想要渲染數(shù)據(jù)的第二個(gè)組件function Bags(props) { return (    <Row>        <CardDeck>            <Col sm={14} md={8} lg={6}>                <Card className='my-3 p-3 rounded'>                    {                    props.url ? (                        <div>                            <Card.Img variant="top" src={ props.url.img || 'holder.js/100px160'} />                            <Card.Body>                                <Card.Title> {props.url.name} </Card.Title>                                <Card.Text>                                This is the greatest albums of rock band Pearl Jam according to Nikolas                                </Card.Text>                            </Card.Body>                        </div>                    ) : (                        <div className="myprogress">                            <CircularProgress color="secondary" />                        </div>                        )                    }                </Card>            </Col>        </CardDeck>    </Row>                           )}對(duì)于第二個(gè)組件,我想根據(jù)我擁有的數(shù)據(jù)數(shù)量生成 Bootstrap-React 卡的數(shù)量。例如,如果我的 JSON 文件中有 6 個(gè)元素,我希望在第二個(gè)組件中生成 6 個(gè) React-bootstrap 卡并為每個(gè)卡打印一些信息,例如名稱。通過上面的代碼,我完成了傳遞道具,但 console.log 的道具不是我的數(shù)據(jù)。這就是我在控制臺(tái)中得到的內(nèi)容console.log(props)誰能告訴我如何正確傳遞我的數(shù)據(jù)或建議更好的方法來做到這一點(diǎn)。我希望我的問題能被理解。我可以提供任何人想要的更多信息
查看完整描述

3 回答

?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

我認(rèn)為這就是您想要實(shí)現(xiàn)的目標(biāo):


function Products() {

  const [url, setUrl] = useState([]);


  useEffect(() => {

    async function asyncCall() {

      const myurl = await axios.get("i put a example link here:mydata.json");

      setUrl(myurl.data);

    }

    asyncCall();

  }, []);


  return (

    <Row>

      {/*{ {url.map((url => (  */}

      {/* the url in the arrow function was shadowing the url array that you were trying to pass to the bags componenet */}

      <Col key={url.id} sm={12} md={6} lg={4} xl={3}>

        <Bags url={url} />

      </Col>


      {/*           )

        ))}  */}

    </Row>

  );

}


function Bags(props) {

  return (

    <Row>

      <CardDeck>

        <Col sm={14} md={8} lg={6}>

          <Card className="my-3 p-3 rounded">

            {props.url.length > 0 ? (

              props.url.map((el) => (

                <div>

                  <Card.Img

                    variant="top"

                    src={el.img || "holder.js/100px160"}

                  />

                  <Card.Body>

                    <Card.Title> {el.name} </Card.Title>

                    <Card.Text>

                      This is the greatest albums of rock band Pearl Jam

                      according to Nikolas

                    </Card.Text>

                  </Card.Body>

                </div>

              ))

            ) : (

              <div className="myprogress">

                <CircularProgress color="secondary" />

              </div>

            )}

          </Card>

        </Col>

      </CardDeck>

    </Row>

  );

}

您能確認(rèn)結(jié)果嗎?



查看完整回答
反對(duì) 回復(fù) 2023-09-07
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

我?guī)缀跤眠@個(gè)實(shí)現(xiàn)解決了問題


function Bags() { 


const [url, setUrl] = useState([]);

//const [myfinal,setFinal] = useState([]);


useEffect(() => {

    async function asyncCall() {

        const myurl = await axios.get("https://mysiteproject-8adcf.firebaseio.com/products.json")

            setUrl(myurl.data)

    }

    asyncCall();


},[]);


if (url) {

    //let myvar = url;

    //console.log(myvar.img);

    //console.log(myvar);

    url.map((url) => console.log(url.img));

}


//console.log()


return (


    <Row>

        <CardDeck>

            <Col sm={14} md={8} lg={6}>

                <Card className='my-3 p-3 rounded'>

                {url.length > 0 ? (

          url.map((el) => (

            <div>

              <Card.Img

                variant="top"

                src={el.img || "holder.js/100px160"}

              />

              <Card.Body>

                <Card.Title> {el.name} </Card.Title>

                <Card.Text>

                  This is the greatest albums of rock band Pearl Jam

                  according to Nikolas

                </Card.Text>

              </Card.Body>

            </div>

          ))

        ) : (

          <div className="myprogress">

            <CircularProgress color="secondary" />

          </div>

        )}

                </Card>

            </Col>

        </CardDeck>

    </Row>

    

                       

)


}

我不知道這個(gè)實(shí)現(xiàn)是否是最佳的


查看完整回答
反對(duì) 回復(fù) 2023-09-07
?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

試試這個(gè)并告訴我是否有效


import React, { useEffect, useState } from "react";

import axios from "axios";

import "./styles.css";


export default function App() {

  const [url, setUrl] = useState([]);


  useEffect(() => {

    async function asyncCall() {

      const response = await fetch(

        "https://mysiteproject-8adcf.firebaseio.com/products.json"

      );

      const responseJson = await response.json();

      console.log(responseJson);

      setUrl(responseJson);

    }

    asyncCall();

  }, []);


  return (

    <div>

      {url.map((url => (

                <Col key={url.id} sm={12} md={6} lg={4} xl={3}>

                    <Bags url={url} />

                </Col>    

                )

        ))}

    </div>

  );

}


查看完整回答
反對(duì) 回復(fù) 2023-09-07
  • 3 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報(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)