如何防止setState()将我的数组转换为字符串?

huangapple go评论59阅读模式
英文:

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(() =&gt; {
    // `existingEvents` is coerced to a string
    console.log(&#39;existingEvents equals: &#39; + existingEvents)
  }, [existingEvents])

It should be:

  useEffect(() =&gt; {
    console.log(&#39;existingEvents equals:&#39;, existingEvents)
  }, [existingEvents])

Also, your second .then() function should be:

.then(() =&gt; setExistingEvents(&#39;&#39;)) // not .then(setExistingEvents(&#39;&#39;)) &lt;- this causes setExistingEvents(&#39;&#39;) 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(&#39;existingEvents equals: &#39; + existingEvents)

If you want to have entire data in a string format, you can use JSON.stringify

console.log(&#39;existingEvents equals: &#39; + JSON.stringify(existingEvents))

Or use a separator , in console.log

console.log(&#39;existingEvents equals: &#39;, existingEvents)

huangapple
  • 本文由 发表于 2023年1月6日 10:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定