Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 文本元素中的React本机调用函数_Reactjs_Firebase_React Native_Google Cloud Firestore - Fatal编程技术网

Reactjs 文本元素中的React本机调用函数

Reactjs 文本元素中的React本机调用函数,reactjs,firebase,react-native,google-cloud-firestore,Reactjs,Firebase,React Native,Google Cloud Firestore,我想调用一个getName函数,它接受一个game.set(Integer)并返回一个字符串。 我想在文本元素中设置的字符串。 该函数工作正常,返回前我打印了结果。 现在,在从“../SetsAPI”导入函数import{getName}之后,调用函数后得到的结果总是未定义 函数调用: import { getName } from '../SetsAPI'; ... <View style={styles.row}> <Text style={styles

我想调用一个
getName
函数,它接受一个
game.set(Integer)
返回一个字符串。
我想在文本元素中设置的字符串。
该函数工作正常,返回前我打印了结果。
现在,在从“../SetsAPI”导入函数
import{getName}之后
,调用函数后得到的结果总是
未定义

函数调用:

import { getName } from '../SetsAPI';
...
    <View style={styles.row}>
      <Text style={styles.text}>Game: </Text>
          <View style={styles.questionAmount}>
               <Text>{getName(game.set)}</Text>
          </View>
     </View>
为什么会发生这种情况?我怎样才能预防呢?
谢谢大家!

您需要使用
useState
存储该值,然后在UI中更新该值

首先在以下地点宣布该州:

{name, setName} = useState('');
这允许我们获取
name
并调用
setName
,并给它一个初始值
'

然后在
componentDidMount
useffect
钩子中执行以下操作:

setsRef
.doc(setId)
.get()
.then((doc) => {
  if (doc.exists) {
    console.log("document name: " + doc.data().name)
    setName(doc.data().name);
  } else {
    console.log("No such document!");
  }
这会导致组件重新渲染自身,然后可以在渲染方法中使用
name
属性

另见:


您需要使用
useState
来存储该值,然后在UI中更新该值

首先在以下地点宣布该州:

{name, setName} = useState('');
这允许我们获取
name
并调用
setName
,并给它一个初始值
'

然后在
componentDidMount
useffect
钩子中执行以下操作:

setsRef
.doc(setId)
.get()
.then((doc) => {
  if (doc.exists) {
    console.log("document name: " + doc.data().name)
    setName(doc.data().name);
  } else {
    console.log("No such document!");
  }
这会导致组件重新渲染自身,然后可以在渲染方法中使用
name
属性

另见: