反应。如何保存到变量中,而不是 console.log()

React . How to save into a variable ,and not in console.log()

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

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

非常感谢。

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

  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
        );

您可能希望将结果放入状态。例如钩子

  const [state, setState] = useState("");


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

现在结果将在状态变量中。