-1

I'm passing data through different pages down to the last page in my app, its been working fine.

But the issue is the last page has 2 components so the typical </ChatActivity navigation="{this.props.navigation}" />, here's what I mean:

I have an App.js

content of App.js

import ChatScreen from './chat'
    class ChatActivity extends Component {
      static navigationOptions = {
        ...
      }
      render() {
        return(
            <ChatScreen navigation={this.props.navigation} />
        )
      }
    }

I also have chat.js that contains the chat component. Chat.js itself, needs to import Fire from './fire.js'

so now, this.props.navigation was only passed to Chat.js...but I need to access it from fire.js as well.

I've read about import {useNavigation}, but from what i have tried it didn't work cause my fire.js doesn't even look like the example in the docs

this is my fire.js

class Fire extends React.Component{
  constructor (props) {
    super(props)

    this.init()
    this.checkAuth()
  }
  init = () => {
        firebase.initializeApp({

        })
  };

  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,
       // image: item.image,
        //video: item.video,
        user: item.user
      }
      this.db.child(`NEED NAVIGATION PARAMS HERE`).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.child(`NEED NAVIGATION PARAMS HERE`).on('child_added', snapshot => callback(this.parse(snapshot)))
  }

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

  get db() {
    return firebase.database().ref(`NEED NAVIGATION PARAMS HERE`);
  }

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

export default new Fire();

Since i couldn't access navigation params, I tried AsyncStorage, but thats probably not the best practice and it isn't working too well. Not sure if its the AsyncStorage or react-native-gifted-chat but when I load the chat page once, it shows the same messages for other chats till I restart the app which shouldn't be cause i'm fetching the data based on unique parameters.

kunlee
  • 591
  • 1
  • 4
  • 15

1 Answers1

1

You have just missed one step here...

Since you have passed the navigation as props by using the following approach:

<ChatScreen navigation={this.props.navigation} />

the chat screen gets to use navigation properties of ChatActivity.

For Fire.js to be able to use the navigation as well, that was provided to Chat.js by ChatActivity you will need to pass the navigation props received by Chat.js to Fire.js in the same way.

This is how your Chat.js should look like:

import Fire from './Fire'

class Chat extends Component {
    static navigationOptions = {
        ...
    }
    render() {
        return(
            <Fire navigation={this.props.navigation} />
        )
    }
}

That should solve the issue. Cheers!

Gavin D'mello
  • 424
  • 4
  • 7
  • Hii, thank you for your answer..just tried it, im getting this error `TypeError: undefined is not an object (evaluating 'this.props.navigation')`...This is how i call it out `constructor (props) { this.init() this.checkAuth() this.state = { userid: this.props.navigation.state.params.userid, sellerid: this.props.navigation.state.params.sellerid } }` – kunlee May 02 '21 at 12:35
  • Are you there ? – kunlee May 02 '21 at 22:55
  • Which screen do you get this error at? Fire.js or Chat.js ? – Gavin D'mello May 03 '21 at 14:29
  • Fire.js .I dont get the error if i dont call navigation props in Fire.js. Dont know if you missed it, but my fire.js is aexported as a function not a class...maybe thats why it doesn't work for it – kunlee May 03 '21 at 19:22
  • @kunlee Review this snack and test it out : https://snack.expo.io/rxuhGMq57 – Gavin D'mello May 04 '21 at 04:47
  • The snack works, but its quite different from what i'm doing, you have 2 screens, i also have 2 screens but one of the screens is importing from another js file..its that other js file thats needs access to props.navigation....[link](https://snack.expo.io/@ad8kunle/my-chat-firebase) - that's my own snack with my code. clicking on an item in `seller.js` leads to `chat.js`...chat.js `imports Fire from fire.js` ...`fire.js` creates the chat room, to do this it needs access to the navigation parameters passed from `seller.js` – kunlee May 04 '21 at 20:35
  • Fixed it by joining both chat.js and fire.js. always knew that would work but i'm new to react-native, so i wanted to know how its really done rather than just cutting corners – kunlee May 05 '21 at 13:31
  • Great to hear mate. – Gavin D'mello May 05 '21 at 20:05
  • 1
    @kunlee As per you question.. my solution provides appropriate solution. So as a contributor, can you flag my solution as the correct accepted answer.. it would really help a lot of other people new to React native as well. – Gavin D'mello May 05 '21 at 20:06