1

Если я пишу так:

render() {
    let a = this.state.table
    for (var key in a) {
        console.log("Key:", key)
        console.log("Value:", a[key])
    }
    ...
}

То все работает правильно и выводит

KEY: 0
Value: {$$typeof: Symbol(react.element), type: "tr", key: "3", ref: null, props: {…}, …}

А так:

render() {
    let a = this.state.table
    a[0] = some_thing \\ a['0'] = some_thing  .table тоже
    ...
}

Выдает ошибку:

Uncaught TypeError: Cannot set property '0' of null (react-dom.development.js:25193)

Как это исправить?

2
  • a[0] = this.state.table - ??
    – user176262
    22 дек 2019 в 16:46
  • @Igor, просто для примера
    – Vlad
    22 дек 2019 в 16:59

1 ответ 1

1

render вызывается несколько раз. В один из этих разов (первый?) this.state.table равно null. Цикл по свойствам null-объекта не выполняет ни одной итерации, поэтому Вы его не видите в консоли.

var a = null;
console.log("before for");
for (var key in a) {
  console.log("Key:", key)
  console.log("Value:", a[key])
}
console.log("after for");

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.