TypeError:未定义不是对象(正在评估“ const.join”)

时间:2019-03-29 13:28:29

标签: firebase react-native expo

如何在状态变量上声明数组

我正在使用react native expo和firebase都是最新的

export default class Profile extends Component {
  state = {
    ageRangeValues: this.props.user.ageRange,
    distanceValue: [this.props.user.distance],
}
render() {
    const {
      ageRangeValues,
      distanceValue,
    } = this.state;

return (
      <View>
      <Slider
        min={5}
        max={100}
        values={distanceValue}
        onValuesChange={val => this.setState({ distanceValue: val })}
        onValuesChangeFinish={val => this.updateUser('distance', val[0])}
      />
    <Slider
        min={18}
        max={70}
        values={ageRangeValues}
        onValuesChange={val => this.setState({ ageRangeValues: val })}
        onValuesChangeFinish={val => this.updateUser('ageRange', val)}
      />
        </View>) }

我希望它能正常工作,但是ageRangeValue是未定义的,但是定义的distanceValue不知道为什么可能是因为ageRangeValue使用ageRange及其数组。如果我声明areRangeValue:[19,20],则一切正常,但是如果我以所有方式定义的方式离开它,则

这是我的预载

const firebaseConfig = {
  apiKey: 'XXXXXXXXX',
  databaseURL: 'XXXXX',
  storageBucket: 'XXXXX',
};

firebase.initializeApp(firebaseConfig);

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    width: null,
    height: null,
    resizeMode: 'contain',
  },
});

export default class Preload extends Component {
  constructor() {
    super();
    this.loadApp();
    // SVGAnimatedLengthList.loadApp();
  }

  authenticate = (token) => {
    const provider = firebase.auth.FacebookAuthProvider
    const credential = provider.credential(token);
    return firebase.auth().signInWithCredential(credential);
  };

_goHome = (user) => {
  const resetAction = StackActions.reset({
    index: 0,
    actions: [NavigationActions.navigate({ routeName: 'Home', params: { user } })],
  });
  this.props.navigation.dispatch(resetAction);
};

  loadApp = async () => {
    //firebase.auth().signOut();
    firebase.auth().onAuthStateChanged((auth) => {
      if (auth) {
        this.firebaseRef = firebase.database().ref('users');
        this.firebaseRef.child(auth.uid).on('value', (snap) => {
          const user = firebase.auth().currentUser;
          if (user != null) {
            this.firebaseRef.child(auth.uid).off('value');
            this._goHome(user);
          }
        });
      } else {
        this.setState({ showSpinner: false });
        this.props.navigation.navigate('Login');
      }
    });
  }

  render() {
    return (
      <ImageBackground source={require('./images/fondo.png')} style={styles.container}>
        <ActivityIndicator />
      </ImageBackground>
    );
  }
}

2 个答案:

答案 0 :(得分:0)

尝试使用构造函数

export default class Profile extends Component {
  constructor(){
    super();
    this.state = {
      ageRangeValues: this.props.user.ageRange,
      distanceValue: [this.props.user.distance],
    };
  }

  render() {

    const { ageRangeValues, distanceValue } = this.state;

    return (
      <View>
        <Slider
          min={5}
          max={100}
          values={distanceValue}
          onValuesChange={val => this.setState({ distanceValue: val })}
          onValuesChangeFinish={val => this.updateUser('distance', val[0])}
        />
        <Slider
          min={18}
          max={70}
          values={ageRangeValues}
          onValuesChange={val => this.setState({ ageRangeValues: val })}
          onValuesChangeFinish={val => this.updateUser('ageRange', val)}
        />
      </View>
    );
  }

答案 1 :(得分:0)

Vencovsky就在传递数据的预览页面上

loadApp = async () => {
    //firebase.auth().signOut();
    firebase.auth().onAuthStateChanged((auth) => {
      if (auth) {
        this.firebaseRef = firebase.database().ref('users');
        this.firebaseRef.child(auth.uid).on('value', (snap) => {
          const user = firebase.auth().currentUser;
          if (user != null) {
            this.firebaseRef.child(auth.uid).off('value');
            this._goHome(user);
          }
        });
      } else {
        this.setState({ showSpinner: false });
        this.props.navigation.navigate('Login');
      }
    });
  }

更改const用户= firebase.auth()。currentUser;到const user = snap.val(); 搞定了