子组件的道具没有得到更新。

我有一个react组件,我们根据标志调用条件。

在ComplianceCards js中,我根据flag runTriggered来划分函数。

runTriggered ? UpdateCards() : createCards()

父组件 ComplianceCards – 当它进入体内 createCards 功能都能正常工作,但当 UpdateCard() 当UpdateCards()被调用时,我看到了我传递的最新值,但在子组件中它仍然显示旧的道具值。

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 => {
                                                return (
                                                    <ComplianceCard
                                                        cardData={newCard}
                                                        key={newCard.complianceType}
                                                        handleClick={this._handleClick}
                                                        storeArchiveData={this.storeArchiveData}
                                                    />
                                                );
                                            })}
                                    </div>
                                </DnxCardLayout>
                            </div>
                        );
                        return view;
                    } else {
                        console.log("updatedCardData else come inside");
                    }
                } else {
                    console.log("else come inside");
                }
            })
            .catch((error)=>{
                console.log ("Let's see what's going on here -> ", error);
            });
        };

当UpdateCards()被调用时,我的控制台打印出———-。

agag data `data`
ComplianceCards.js:504 if come inside

我可以看到这里的数据是最新的更新值,但是在父子组件里面却是旧的值或道具。

我的父组件 ComplianceCards

export default class ComplianceCards extends Component {

    /**
     * Constructor
     * @param {} props
     */
    constructor(props) {
        super(props);
        this.state = {
            // some states
        };
    }

    /**
     * Component DidMount
     */
    componentDidMount() {
    }

    /**
     * Component will un-mount
     */
    componentWillUnmount() {
    }

    /**
     * render function
     */
    render() {
    }
}

我已经根据评论和明确的理解重构。我希望这将有助于。

这是我的Child组件的ComplianceCard代码–。

import React, { Component } from "react";
// some other dependencies

export default class ComplianceCard extends Component {
    constructor(props) {
        super(props);
        this.state = {
            latestArchivedVersion: null,
            networkProfiles: null,
            showImageLoader:false,
            showConfigLoader:false
        };
    }

    /**
     * Get data for each complianceType
     */
    componentDidMount() {
        let { cardData, storeArchiveData } = this.props;
        const complianceType = cardData.complianceType;
        const deviceUuid = cardData.deviceUuid;
        if (complianceType == labels.runningConfig) {
            this.setState( {
                showConfigLoader:true
            })
            getArchiveConfigDetails(deviceUuid).then(data => {
                if (data) {
                    this.setState({
                        latestArchivedVersion: data[data.length - 1]
                    });
                    if (storeArchiveData) {
                        storeArchiveData(data);
                    }
                    this.setState( {
                        showConfigLoader:false
                    })
                }
            });
        } else if (complianceType == labels.networkProfile) {
            // TODO, do we really need this? we need to re-trigger the details api to get all NETWORK_PROFILE data
            getComplianceDetails(deviceUuid, {
                complianceType: labels.networkProfile
            }).then(data => {
                if (data) {
                    this.setState({
                        networkProfiles: [...data]
                    });
                }
            });
        } else if (complianceType == labels.softwareImage) {
            this.setState( { 
                showImageLoader: true
            });
            getImageDetails(deviceUuid).then(data => {
                if (data) {
                    this.setState({
                        imageDetails: data,
                        showImageLoader: false
                    });
                }
            });
        }
    }

    componentDidUpdate(prevProps) {
        if (prevProps.cardData !== this.props.cardData) {
            let { cardData } = this.props;
        }
    }

    componentWillReceiveProps(nextProps) {
        this.setState({ data: nextProps.data });  
      }

    /**
     * Component will un-mount
     */
    componentWillUnmount() {

    }

    /**
     * Handles click event
     * @param {*} target
     */
    handleClick = target => {
        let { cardData, handleClick } = this.props;
        const complianceType = cardData.complianceType;
        handleClick(complianceType, cardData);
    };


    /**
     * Component render function
     */
    render() {
        let { cardData } = this.props;
        // render function code
    }
}

请指出这个问题,谢谢。

解决方案:

你没有从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;
            }
        };
    ```

给TA打赏
共{{data.count}}人
人已打赏
未分类

使用Javascript过滤函数输出,只要它们是真的就可以了(Javascript算法)

2022-9-8 22:22:39

未分类

Spring Maven - 无法生成WSDL。

2022-9-8 22:22:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索