简体   繁体   English

是否可以在本机反应中从另一个 class 组件访问 useRef 变量?

[英]Is it possible to access a useRef variable from another class component in react native?

I am attempting to access a useRef variable declared in my functional component in a class component.我正在尝试访问在 class 组件中的功能组件中声明的 useRef 变量。 Is this possible?这可能吗? Currently when I import the functional component, DetailsScreen, below in my class component, FireMessage, and add console.log(DetailsScreen.receiverUid) in the get db() function it returns 'undefined'.目前,当我在我的 class 组件 FireMessage 中导入功能组件 DetailsScreen 时,并在 get db() function 中添加 console.log(DetailsScreen.receiverUid) 它返回“未定义”。 Do you know what may be causing this and how I can gain access to this variable in a class component?您知道是什么原因造成的,以及如何在 class 组件中访问此变量吗?

**DetailsScreen**


import 'react-native-gesture-handler';

import { Image, StyleSheet, Text, TouchableOpacity, View, ScrollView, 
SafeAreaView, FlatList, Alert, TouchableHighlight, Platform, Dimensions, 
Linking} from 'react-native';

import React, {useState, useContext, useRef} from 'react';


const DetailsScreen = ({route, navigation}) => {

    const receiverUid = useRef(route.params.key);
}


export default DetailsScreen;


**FireMessage**

import firebase from "firebase";
import firebaseConfig from './firebaseConfig.js';
import 'react-native-gesture-handler';
import * as React from 'react';
import DetailsScreen from './DetailsScreen.js';

class FireMessage {

 constructor() {
   this.init()
   this.checkAuth() 
 }

 init = () => {
   if (!firebase.apps.length && firebaseConfig.apps.length === 0) {
   firebase.initializeApp({
    apiKey: "xxxxxxxxxxx",
    authDomain: "xxxxxxxxxxx",
    databaseURL: "xxxxxxxxxx",
    projectId: "xxxxxxxx",
    storageBucket: "xxxxxxxxxx",
    messagingSenderId: "xxxxxxxx",
    appId: "xxxxxxxxxxx",
    measurementId: "xxxxxxxxx"
   });
  }
};

checkAuth = () => {
  firebase.auth().onAuthStateChanged(user => {
    if (!user) {
      firebase.auth().signInAnonymously();
    }
  });
};

send = messages => {
  messages.forEach(item => {
    const message = {
      text: item.text,
      timestamp: firebase.database.ServerValue.TIMESTAMP,
      user: item.user
    };

   this.db.push(message)
 });
};

parse = message => {
   const { user, text, timestamp } = message.val();
   const { key: _id } = message;
   const createdAt = new Date(timestamp);


   return {
    _id,
    createdAt,
    text,
    user
   };
};

get = callback => {
   this.db.on("child_added", snapshot => 
   callback(this.parse(snapshot)));
};

off() {
   this.db.off();
}

get db() {
   console.log(DetailsScreen.receiverUid)
   return firebase.database().ref("messages");
}

get uid() {
   return (firebase.auth().currentUser || {}).uid;
}
}

export default new FireMessage();

Whenever your update receiverUid update DetailsScreen.receiverUid每当你更新receiverUid更新DetailsScreen.receiverUid

const newId = 'some-receiver-id'
receiverUid.current = newId
DetailScreen.receiverUid = newId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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