我有一个带有主 node "user"的实时数据库,在其中我有3个子 node ,这3个子 node 又有4个子 node ,每个子 node .4个 node 中的一个是录音,一个是图像,其中两个是字符串.我试图用"下一步"和"上一步"按钮动态地获取它们,按下"下一步"时,屏幕上会显示下一个 node 的数据.

我使用useState动态更改数据库(ref)的路径,但按下next/back按钮时,屏幕上的数据不会更新.后来我发现,当我刷新/重写ref()时,按下next/back按钮.在功能上,我的数据会得到更新,但我必须为每家媒体做这件事.

这是我的apply.js代码:

import Sound from 'react-native-sound';
import database from '@react-native-firebase/database';
import storage from '@react-native-firebase/storage';
import React , {useEffect, useState} from 'react';
import {
  ScrollView,
  StyleSheet,
  Alert,
  Text,
  View,
  Image,
  Button
} from 'react-native';

const App = () => {
 
  const [myData,setData] = useState({
    letter:'',
    pronun:'',
    word:'',
    image:''
  });
  const [img,setimg] = useState(null);
  const [pronunn,setpronun] = useState(null);
  const [hey,sethey] = useState(1);


  useEffect(() => {
    getDatabase();   
}, []);
  
  function getDatabase() {
    
database().ref('users/'+hey+'/').on('value' , (snapshot) => {
  Sound.setCategory('Playback', true);
  var poo=new Sound(snapshot.val().pronun);
  setData({
    letter: snapshot.val().letter,
    word: snapshot.val().word,
    image: setimg(snapshot.val().image),
    pronun: setpronun(poo)
  });
  console.log(snapshot.val());
});
 }

  return (

<View style={{flex:1, backgroundColor:'#000000', alignContent:'center', alignItems:'center', justifyContent:'center'}}>
  

      <ScrollView>
      <Text style={{color:'#ffff'}}>
    Letter: {myData ? myData.letter : 'loading...' }
  </Text>

  <Text style={{color:'#ffff'}}>
    Word: {myData ? myData.word : 'loading...' }
  </Text>
  <Image style={{width:200, height:200}} 
  source={{uri: img}} 
    />

   <View>
     <Button
     title='Pronunciation'
     onPress={() => {
       return pronunn.play();
     }}
     >

     </Button>

     <Button title='Next' onPress={
       
       () => {
         if (hey>2) {
          Alert.alert('no more records');
         }
         else {

       return sethey(hey+1);
  
         }
       }
      }
       >

       </Button>

       <Button title='back' onPress={
       
       () => {
         if (hey<2) {
          Alert.alert('no more records to go back');
         }
         else {
       return sethey(hey-1);
         }
       }
      }
       >

       </Button>
       </View>
      </ScrollView>

    </View>
);
};

export default App;

推荐答案

由于setData hook/effect取决于hey状态,因此需要在useEffect中将后者指定为数据加载的依赖项.

useEffect(() => {
  getDatabase();   
}, [hey]);

另见:

Javascript相关问答推荐

如何通过继承contentitable属性的元素捕捉keydown事件?

在JavaScript中,如何将请求的回调函数的结果合并到运行的代码中?

脚本.js:3:20未捕获的类型错误:无法读取空的属性(读取addEventHandler)

docx.js:如何在客户端使用文档修补程序

当作为表达式调用时,如何解析方法decorator 的签名?

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

数字时钟在JavaScript中不动态更新

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

在JS中拖放:检测文件

S文本内容和值不必要的不同

material UI按钮组样式props 不反射

IF语句的计算结果与实际情况相反

查询参数中的JAVASCRIPT/REACT中的括号

将Node.js包发布到GitHub包-错误ENEEDAUTH

如何将zoom 变换应用到我的d3力有向图?

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

Next.js无法从外部本地主机获取图像

每隔3个项目交替显示,然后每1个项目交替显示

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

Firebase函数中的FireStore WHERE子句无法执行