英文:
How do I keep setState() from stringifying my array?
问题
我有一个函数,名为 eventQuery,用于从 Firebase 数据库中获取数据。我能够看到数据已成功提取,并返回了我理解为由字符串和对象组成的数组数组。首先,我的代码如下:
const [activeDate, setActiveDate] = useState(new Date())
const [existingEvents, setExistingEvents] = useState({})
useEffect(() => {
eventQuery(activeDate)
.then(data => {
setExistingEvents(data);
console.log(data)
})
.then(setExistingEvents(''))
.catch(err => console.log('no events'))
}, [activeDate])
useEffect(() => {
console.log('existingEvents equals: ' + existingEvents)
}, [existingEvents])
在 promise 中的 console.log(data) 的输出形式如下:
[["String1.1", {"String1.2": "String1.3", "String1.4": "String1.5", "String1.6": "String1.7"}], ["String2.1", {etc.}], ["String3.1", {etc.}]]
然而,当我使用 setExistingEvents(data) 将该数组数组传递给 existingEvents,然后随后将新值记录到控制台,结果如下:
existingEvents equals: String1.1,[object Object],String2.1,[object Object],String3.1,[object Object]
据我所知,setState() 实际上是在我的数组数组上运行 toString()。我是否做错了什么,还是这是函数的固有部分?非常感谢你的帮助。
英文:
I have a function, eventQuery which fetches data from a Firebase DB. I am able to see the data is successfully pulled and returns what I understand to be an array of arrays each composed of a string and an object. First, my code:
const [activeDate, setActiveDate] = useState(new Date())
const [existingEvents, setExistingEvents] = useState({})
useEffect(() => {
eventQuery(activeDate)
.then(data => {
setExistingEvents(data);
console.log(data)
})
.then(setExistingEvents(''))
.catch(err => console.log('no events'))
}, [activeDate])
useEffect(() => {
console.log('existingEvents equals: ' + existingEvents)
}, [existingEvents])
The output of console.log(data) within the promise yields something in the form of:
[["String1.1", {"String1.2": "String1.3", "String1.4": "String1.5", "String1.6": "String1.7"], ["String2.1", {etc.}], ["String3.1", {etc.}]]
However, when I use setExistingEvents(data) to pass that array of arrays to existingEvents, and then I subsequently console log the new value, the result is:
existingEvents equals: String1.1,[object Object],String2.1,[object Object],String3.1,[object Object]
As far as I can tell, setState() is essentially running toString() on my array of arrays. Am I doing something wrong or is this an inherent part of the function? Thanks so much for your help.
答案1
得分: 1
Your console.log
statement is actually the reason why the output looks like existingEvents
was stringified (setState doesn't stringify the argument).
Instead of:
useEffect(() => {
// `existingEvents` is coerced to a string
console.log('existingEvents equals: ' + existingEvents)
}, [existingEvents])
It should be:
useEffect(() => {
console.log('existingEvents equals:', existingEvents)
}, [existingEvents])
Also, your second .then()
function should be:
.then(() => setExistingEvents('')) // not .then(setExistingEvents('')) <- this causes setExistingEvents('') to be called immediately
英文:
Your console.log
statement is actually the reason why the output looks like existingEvents
was stringified (setState doesn't stringify the argument).
Instead of:
useEffect(() => {
// `existingEvents` is coerced to a string
console.log('existingEvents equals: ' + existingEvents)
}, [existingEvents])
It should be:
useEffect(() => {
console.log('existingEvents equals:', existingEvents)
}, [existingEvents])
Also, your second .then()
function should be:
.then(() => setExistingEvents('')) // not .then(setExistingEvents('')) <- this causes setExistingEvents('') to be called immediately
答案2
得分: 1
问题在于您在这里使用字符串附加:
console.log('existingEvents equals: ' + existingEvents)
如果您想要将整个数据以字符串格式输出,您可以使用 JSON.stringify
:
console.log('existingEvents equals: ' + JSON.stringify(existingEvents))
或者在 console.log
中使用逗号分隔符:
console.log('existingEvents equals: ', existingEvents)
英文:
Your problem is you're using a string append on this
console.log('existingEvents equals: ' + existingEvents)
If you want to have entire data in a string format, you can use JSON.stringify
console.log('existingEvents equals: ' + JSON.stringify(existingEvents))
Or use a separator ,
in console.log
console.log('existingEvents equals: ', existingEvents)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论