ReactJS comapre componentdidupdate内部的两个对象

时间:2019-02-23 03:36:53

标签: react-redux

我正在尝试在componentdid更新中基于以前的道具和当前道具添加类,但是它没有给出预期的确切结果,还显示语法错误。

下面是我的代码

componentDidUpdate(prevProps, prevState){
        const prev = prevProps.marketcap.map( (coin, i) => (
            {
                coin.DISPLAY.USD.PRICE < this.props.marketcap[i].DISPLAY.USD.PRICE ? 'ups' : 'down'
            }
        ));
        console.log(prev); //it will return an object containing ups or down..
    }

marketcap以上是道具

1 个答案:

答案 0 :(得分:0)

您的代码中有语法错误,三元条件周围的花括号。这应该起作用:

componentDidUpdate(prevProps, prevState){
    const prev = prevProps.marketcap.map((coin, i) => (
       coin.DISPLAY.USD.PRICE < this.props.marketcap[i].DISPLAY.USD.PRICE ? 'ups' : 'down'
    ));
    console.log(prev); //it will return an object containing ups or down..
}