0
votes

I know there have been similar questions, but I tried anyway and still can't find the solution.

I have a login screen, registration, chat and a test screen (where I use just to try to fix this error) I want to pass the name, email and avatar data that I get there from Login / Register, but it is returned to me that the object is undefined. I only corrected the error if I put the navigation as follows: Login or Register> Chat. However, I do not want to do so, I need to click on an image and direct to this chat. Apparently it's a common mistake, but I'm frustrated with trying to fix it (I'm new to React-Native)

Chat.js

import { GiftedChat } from "react-native-gifted-chat"; // 0.3.0

import firebaseSvc from "../FirebaseSvc";

type Props = {
  name?: string,
  email?: string,
  avatar?: string
};

class Chat extends React.Component<Props> {
  constructor(props) {
    super(props);
  }
  static navigationOptions = ({ navigation }) => ({
    title: (navigation.state.params || {}).name || "Chat!"
  });

  state = {
    messages: []
  };

  get user() {
    return {
      name: this.props.navigation.state.params.name,
      email: this.props.navigation.state.params.email,
      avatar: this.props.navigation.state.params.avatar,
      id: firebaseSvc.uid,
      _id: firebaseSvc.uid // need for gifted-chat
    };
  }

  render() {
    return (
      <GiftedChat
        messages={this.state.messages}
        onSend={firebaseSvc.send}
        user={this.user}
      />
    );
  }

  componentDidMount() {
    firebaseSvc.refOn(message =>
      this.setState(previousState => ({
        messages: GiftedChat.append(previousState.messages, message)
      }))
    );
  }
  componentWillUnmount() {
    firebaseSvc.refOff();
  }
}

export default Chat;

Login.js

import { Constants, ImagePicker, Permissions } from "expo";
import {
  StyleSheet,
  Text,
  TextInput,
  TouchableOpacity,
  KeyboardAvoidingView,
  View,
  Button,
  ImageEditor,
  Image,
  StatusBar,
  LayoutAnimation
} from "react-native";
import firebaseSvc from "../FirebaseSvc";
import firebase from "firebase";
import { auth, initializeApp, storage } from "firebase";
import uuid from "uuid";

class Login extends React.Component {
  state = {
    name: "",
    email: "",
    password: "",
    avatar: ""
  };
  // using Fire.js
  onPressLogin = async () => {
    console.log("pressing login... email:" + this.state.email);
    const user = {
      name: this.state.name,
      email: this.state.email,
      password: this.state.password,
      avatar: this.state.avatar
    };

    const response = firebaseSvc.login(
      user,
      this.loginSuccess,
      this.loginFailed
    );
  };

  loginSuccess = () => {
    console.log("login successful, navigate to chat.");
    this.props.navigation.navigate("TelaTeste", {
      name: this.state.name,
      email: this.state.email,
      avatar: this.state.avatar
    });
  };
  loginFailed = () => {
    console.log("login failed ***");
    alert("Login failure. Please tried again.");
  };

  onChangeTextEmail = email => this.setState({ email });
  onChangeTextPassword = password => this.setState({ password });

  render() {
    LayoutAnimation.easeInEaseOut();

    return (
      <View style={styles.container}>
        <KeyboardAvoidingView behavior="padding">
          <Text style={styles.titulo}>{"CUDDLE"}</Text>

          <View style={styles.errorMessage}>
            {this.state.errorMessage && (
              <Text style={styles.error}>{this.state.errorMessage}</Text>
            )}
          </View>

          <View style={styles.form}>
            <View>
              <Text style={styles.inputTitle}>Email Address</Text>
              <TextInput
                style={styles.input}
                autoCapitalize="none"
                onChangeText={this.onChangeTextEmail}
                value={this.state.email}
              ></TextInput>
            </View>

            <View style={{ marginTop: 32 }}>
              <Text style={styles.inputTitle}>Password</Text>
              <TextInput
                style={styles.input}
                secureTextEntry
                autoCapitalize="none"
                onChangeText={this.onChangeTextPassword}
                value={this.state.password}
              ></TextInput>
            </View>
          </View>

          <TouchableOpacity style={styles.button} onPress={this.onPressLogin}>
            <Text style={{ color: "#FFF", fontWeight: "500" }}>Sign in</Text>
          </TouchableOpacity>

          <TouchableOpacity
            style={{ alignSelf: "center", marginTop: 32 }}
            onPress={() => this.props.navigation.navigate("Register")}
          >
            <Text style={{ color: "#414959", fontSize: 13 }}>
              New to SocialApp?{" "}
              <Text style={{ fontWeight: "500", color: "#5B2B80" }}>
                Sign Up
              </Text>
            </Text>
          </TouchableOpacity>
        </KeyboardAvoidingView>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  titulo: {
    color: "#5B2B80",
    fontSize: 30,
    fontWeight: "bold",
    textAlign: "center",
    marginTop: 150
  },
  errorMessage: {
    height: 72,
    alignItems: "center",
    justifyContent: "center",
    marginHorizontal: 30
  },
  error: {
    color: "#5B2B80",
    fontSize: 13,
    fontWeight: "600",
    textAlign: "center"
  },
  form: {
    marginBottom: 48,
    marginHorizontal: 60
  },
  inputTitle: {
    color: "#8A8F9E",
    fontSize: 10,
    textTransform: "uppercase"
  },
  input: {
    borderBottomColor: "#8A8F9E",
    borderBottomWidth: StyleSheet.hairlineWidth,
    height: 40,
    fontSize: 15,
    color: "#161F3D"
  },
  button: {
    marginHorizontal: 60,
    backgroundColor: "#5B2B80",
    borderRadius: 4,
    height: 52,
    alignItems: "center",
    justifyContent: "center"
  }
});

export default Login;

App.js

import Login from "./components/Login";
import CreateAccount from "./components/CreateAccount";
import Chat from "./components/Chat";
import TelaTeste from "./components/TelaTeste";
// Import React Navigation
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";

const AppNavigator = createStackNavigator({
  Login: { screen: Login },
  CreateAccount: { screen: CreateAccount },
  Chat: { screen: Chat },
  TelaTeste: TelaTeste
});

export default createAppContainer(AppNavigator);
1

1 Answers

1
votes

Change, to use arrow function, this inside user() refers to a different thing

get user() {
  return {
    name: this.props.navigation.state.params.name,
    email: this.props.navigation.state.params.email,
    avatar: this.props.navigation.state.params.avatar,
    id: firebaseSvc.uid,
    _id: firebaseSvc.uid // need for gifted-chat
  };
}

to

user = () => {
  return {
    name: this.props.navigation.state.params.name,
    email: this.props.navigation.state.params.email,
    avatar: this.props.navigation.state.params.avatar,
    id: firebaseSvc.uid,
    _id: firebaseSvc.uid // need for gifted-chat
  };
}