简体   繁体   中英

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

I'm very new to React. I can see the right result with the following function using console.log(), but how can I save the result (it's a number, not an array), into a simple variable.

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

thanks a lot.

Are you utilizing states? if so then what you can do is initialize states above like this :

  const [
        snapShot,
        setSnapShot,
    ] = useState()

and use

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

if its in somewhere you cant use a state, just use

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

You might want to put the result into a state. For example with hooks

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


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

Now the result will be in the state variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM