【发布时间】:2020-08-11 07:24:52
【问题描述】:
我正在尝试将一些数据作为字典/JSON 存储在 React-Native 中的 JS 中。
我已经这样定义了字典:
const [item, setItem] = useState({});
function onUpdate(id, quantity) {
console.debug('tab index : ' + tabIndex);
console.debug('id : ' + id);
console.debug('quantity : ' + quantity);
console.debug(item);
item[tabIndex][id] = quantity;
console.debug(item);
//I'm not setting it
//I have even tried declaring a second object, like :
//let i=item
//It also causes the same problem
}
在日志中我得到:
[Tue Aug 11 2020 13:19:13.195] DEBUG tab index : 1
[Tue Aug 11 2020 13:19:13.198] DEBUG id : 1
[Tue Aug 11 2020 13:19:13.202] DEBUG quantity : 1
[Tue Aug 11 2020 13:19:13.203] DEBUG {}
[Tue Aug 11 2020 13:19:13.204] ERROR TypeError: undefined is not an object (evaluating 'item[tabIndex][id] = quantity')
这是为什么呢?
【问题讨论】:
标签: javascript json react-native