首页 > 解决方案 > 反应。如何保存到变量中,而不是在 console.log()

问题描述

我对 React 很陌生。我可以使用 console.log() 使用以下函数看到正确的结果,但是如何将结果(它是一个数字,而不是数组)保存到一个简单的变量中。

firestore.collection('games').where('user_ide', '==', uid).get().then( (snapshot) => 
    console.log(snapshot.docs.length),
    );

多谢。

标签: reactjsreact-reduxconsole.log

解决方案


你在使用状态吗?如果是这样,那么您可以做的是像这样初始化上面的状态:

  const [
        snapShot,
        setSnapShot,
    ] = useState()

并使用

firestore.collection('games').where('user_ide', '==', uid).get().then( (snapshot) => 
    setSnapShot(snapshot.docs.length),
    );

如果它在某个地方你不能使用状态,只需使用

let snapShot 
firestore.collection('games').where('user_ide', '==', uid).get().then( (snapshot) => 
        snapShot= snapshot.docs.length
        );

推荐阅读