自适应卡多次渲染

时间:2019-05-30 09:39:19

标签: javascript reactjs adaptive-cards

无论何时我们调用setState方法或重新初始化组件,我都会动态地将json与自适应卡绑定在一起,也尝试进行静态操作, 与UI绑定的自适应卡的多个实例。

<AdaptiveCard payload={this.state.adapJson} />

class Home extends PureComponent {


    constructor() {
        super();
        this.state = {
            name: '',
            response: {},
            adaJson: {},
            renderFlag : false,
            weather: {},
            tempt: '',
            tempDesc: ''
        };
    }
    getTime() {
        var myDate = new Date(); var greet = "";
        var hrs = myDate.getHours();
        if (hrs < 12)
            greet = "Good Morning";
        else if (hrs >= 12 && hrs <= 17)
            greet = "Good Afternoon";
        else if (hrs >= 17 && hrs <= 24)
            greet = "Good Evening";
        return greet;
    }
    componentDidMount() {
        this.showPosition();

        // if (navigator.geolocation) {
        //   navigator.geolocation.getCurrentPosition(this.showPosition, this.err);

        //   }        
    }


 showPosition() {
    //some code          
    }
    render() {
        var d = new Date()

        this.state.adaJson = {
            "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
            "type": "AdaptiveCard",
            "version": "1.0",
            "body": [
                {   
                    ..data..
                }
            ]
        };

        return (<div className="App">
                        <html...>    
                    {this.state.tempt ? <AdaptiveCard payload={this.state.adaJson} /> : ""}


        )

    }
}

enter image description here

当我一次又一次单击“主页”按钮时发生(同一页面路由) 如何解决?

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为您在componentDidMount()时呈现了脚本。这样,当您返回等时,渲染是相同的。

您可以使用componentWillUnmount()删除渲染。

再见^^