1

以下の lib を使用して、すべての ApiRequest のコールバック (onSuccess、onError) を実装しています。しかし、イベントがトリガーされたときに状態を更新するときに問題があります。基本ロジックを保持するだけで、すべてのものを削除しようとしました。なぜエラーになるのかわかりません。ライブラリ: https://www.npmjs.com/package/react-native-simple-events

以下は私のコードです

ApiRequest.js

import Events from 'react-native-simple-events';
export function login(email, password) {
       Events.trigger('LoginSuccess', 'response');
}

ログイン.js

import React, { Component, } from 'react'
import {
  View,
  Text,
} from 'react-native'
import Events from 'react-native-simple-events';

import * as request from '../../network/ApiRequest'
class LoginScreen extends Component {

  static propTypes = {}

  static defaultProps = {}

  constructor(props) {
    super(props)
    this.state = {
      status: "new"
    }
  }

  componentDidMount() {
    Events.on('LoginSuccess', 'myID', this.onLoginSuccess); 
    request.login("abc","def")

  }
    componentWillUnmount() {

      Events.rm('LoginSuccess', 'myID');

    }

  onLoginSuccess(data){ 
    this.setState({ //=>error here
       status : "done"
    });
  }
  render() {
    return (
      <View>
        <Text>
          {this.state.status}
        </Text>
      </View>
    )
  }
}

さらに情報が必要な場合はお知らせください

4

1 に答える 1

1

onLoginSuccessこれをメソッドにバインドする必要があります。

Events.on('LoginSuccess', 'myID', this.onLoginSuccess.bind(this));
于 2016-05-31T13:41:04.077 に答える