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

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

子組件的道具沒有更新

子組件的道具沒有更新

呼如林 2022-07-15 10:28:16
我有一個反應組件,我們稱之為基于標志的條件。在 ComplianceCards js 中 - 我根據標志 runTriggered 劃分了功能。runTriggered ? UpdateCards() : createCards()ParentComponent ComplianceCards- 當它進入createCards函數時一切正常,但是當UpdateCard()被調用時,我看到我正在傳遞的最新值,但在子組件中它仍然顯示舊的道具值。const UpdateCards = () => {            let view;            /**             * @param {*} deviceId             * get the compliance details             */            let agag = getComplianceDetails(this.props.deviceId)            .then(parsedData => {                if (parsedData) {                    return parsedData;                }                else {                    console.log ("This function is not returning anything. Let's check why!")                }            })            .catch((error)=>{                console.log ("Let's see what's going on here -> ", error);            });            agag.then(data => {                console.log("agag data", data);                if (data) {                    this.setState({                        updatedCardData: data                    });                    const { updatedCardData } = this.state                    if (updatedCardData) {                        console.log("if come inside");                        view = (                            <div>                                <DnxCardLayout name="cardlayout" style={{ padding: "5px" }}>                                    <div className="cards_flex_container">                                        {updatedCardData &&                                            updatedCardData.map(newCard => {當 UpdateCards() 被調用時,我的控制臺打印 -agag data `data`ComplianceCards.js:504 if come inside我可以在這里看到數據是最新更新的值,但在 s\child 組件中它是舊值或道具。我已經根據評論進行了重構,以便清楚地理解。我希望這將有所幫助。
查看完整描述

2 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

你沒有從你的 if 子句中返回任何東西,因此 React 不會開始渲染這些卡片。


const createCards = runTriggered => {

            let newCards, view = null;

            /**

             * @param {*} deviceId

             * get the compliance details

             */

            let agag = getComplianceDetails(this.props.deviceId).then(data => {

                if (data) {

                    return data;

                }

                // .then afterwards will fail if no data is present

            });

            if (runTriggered) {

                // No return here, assuming createCards is treated as a promise you can try:

                // return agag.then(...

                agag.then(data => {

                    newCards = data;

                    view = (...);

                    return view;

                });

            } else {

                view = (...);

                return view;

            }

        };

    ```


查看完整回答
反對 回復 2022-07-15
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

let agag = getComplianceDetails(this.props.deviceId)

    .then(data => data.json())

    .then(parsedData =>{

        if (parsedData) {

            return parsedData;

        }

        else{

            console.log ("This function is not returning anything. Let's check why!")

        }

    })

    .catch((error)=>{

        console.log ("Let's see what's going on here -> ", error);

    });


查看完整回答
反對 回復 2022-07-15
  • 2 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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