ошибки видимости внутри реагируют на режим subpub, я обновляю некоторые элементы внутри функции подписки и получаю неправильное состояние - PullRequest
0 голосов
/ 10 октября 2018

Я использую реагирование 16.5 с подпапкой lib.

componentDidMount() {
    that = this;
    mystate = this.state.OrderItems;
    PubSub.subscribe(this.props.topic, this.addData);
}

addData(msg, data) {

    for (let i of data) {
        console.log(i);
        console.log(mystate);
        let exist_item = mystate.filter(item => item["product_id"] == i["id"]);
        console.log(exist_item);
        if (exist_item.length >= 1) {
            exist_item[0]["amount"] += 1;
        } else { 
            let tmp = {
                "id": v4(),
                "name": i["name"],
                "amount": 1,
                "item_price": i["price"],
                "product_id": i["id"]
            };
            console.log(tmp);
            mystate.push(tmp);
            console.log(mystate);

        }
    }
    that.setState({
        OrderItems: mystate
    });                   ////1.here I change update the state and rerend
}

componentDidUpdate() { 
    that = this;                  ///2.odd thing happens here
    mystate = this.state.OrderItems;
    console.log(this.state.OrderItems);///here I get []
    console.log(mystate);             ///here I get []
}

Функция componentDidUpdate (), которую я получаю:

Card.js:65 []
Card.js:66 []

Я уже обновляю массив mystate, но когда я проверяю вcomponentnentDidUpdate (), там есть пустая строка.

ps: я не делаю привязку, как PubSub.subscribe(this.props.topic, this.addData.bind(this));, потому что я получил огромную внутреннюю ошибку в привязке

...