Fetch -React Native.Error:on function

时间:2017-03-21 07:27:54

标签: react-native

undefined不是一个函数(评估'this_submitForm()')。 ... *我在调用函数时遇到这个错误。我是新的反应原生,我搜索过每一个地方但我没有得到任何解决方案。如果解决方案可用,请添加示例代码以便更好地理解。

  • 我在这里发布我的代码:*

    var React = require('react-native');
    
    var {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      Navigator,
      TouchableOpacity,
    } = React;
    
     var SCREEN_WIDTH = require('Dimensions').get('window').width;
     var PageOne = React.createClass({
     onEmailChange(email) {
        let s = this.state;
        s.email = email;
        this.setState(s);
    },
    
      render() {
        return (
          <View style={[styles.container, {backgroundColor: 'green'}]}>
            <Text style={styles.welcome}>Greetings!</Text>
            <TouchableOpacity onPress={this._handlePress}>
              <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}>
               <TextInput
                     onChangeText={this.onEmailChange}
                     placeholderTextColor="#a0a0a0"
                     placeholder="email"
                     underlineColorAndroid='transparent'/>
              </View>
            </TouchableOpacity  onPress={this._submitForm()}>
           </View>
        )
      },
    });
    
     var PageTwo = React.createClass({
    
      render() {
        return (
          <View style={[styles.container, {backgroundColor: 'green'}]}>
            <Text style={styles.welcome}>Greetings!</Text>
            <TouchableOpacity onPress={this._handlePress}>
              <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}>
                <Text style={styles.welcome}>Go to page two</Text>
              </View>
            </TouchableOpacity>
           </View>
        )
      },
    });
    
    class VerifyMe extends Component {
    
    constructor(props) {
        super(props);
    
        this.state = {
            email: '',
            password: '',
        }
    }
    
    _renderScene(route, navigator) {
        if (route.id === 1) {
            return <PageOne navigator={navigator}/>
        } else if (route.id === 2) {
            return <PageTwo navigator={navigator}/>
        } 
    }
    

    _submitForm(){     fetch(baseURL +'users / loginUser',{         方法:'post',         body:JSON.stringify({             config_name:'default',             电子邮件:this.state.email,

        })
            .then((response) => response.json())
            .then((responseJson) => {
                if (response.data.responsecode === 1) {
                    this.props.navigator.push({id: 2,});
                }
            })
            .catch((error) => {
                console.error(error);
            })
    });
    

    }     render(){         回来(                      );     } }

1 个答案:

答案 0 :(得分:1)

您正试图在PageOne中使用this._submitFrom但是您在PageTwo中定义了它,尝试使用以下代码

&#13;
&#13;
var React = require('react-native');

var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  TouchableOpacity,
} = React;

 var SCREEN_WIDTH = require('Dimensions').get('window').width;
 var PageOne = React.createClass({
 getInitialState () {
    return {
       email:'', //add your initial state for this class here
  };
},
 onEmailChange(email) {
    //let s = this.state;
    //s.email = email;          // no need of these lines
    this.setState({
      email:email
    });
},
_submitForm() { 
fetch(baseURL+'users/loginUser', { 
method: 'post', body: JSON.stringify({ config_name: 'default', email: this.state.email
})
        .then((response) => response.json())
        .then((responseJson) => {
            if (response.data.responsecode === 1) {
                this.props.navigator.push({id: 2,});
            }
        })
        .catch((error) => {
            console.error(error);
        })
});
}

  render() {
    return (
      <View style={[styles.container, {backgroundColor: 'green'}]}>
        <Text style={styles.welcome}>Greetings!</Text>
        <TouchableOpacity onPress={this._handlePress}>
          <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}>
           <TextInput
                 onChangeText={this.onEmailChange}
                 placeholderTextColor="#a0a0a0"
                 placeholder="email"
                 underlineColorAndroid='transparent'/>
          </View>
        </TouchableOpacity  onPress={this._submitForm.bind(this)}> //need to use bind
       </View>
    )
  },
});

 var PageTwo = React.createClass({

  render() {
    return (
      <View style={[styles.container, {backgroundColor: 'green'}]}>
        <Text style={styles.welcome}>Greetings!</Text>
        <TouchableOpacity onPress={this._handlePress}>
          <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}>
            <Text style={styles.welcome}>Go to page two</Text>
          </View>
        </TouchableOpacity>
       </View>
    )
  },
});

class VerifyMe extends Component {

constructor(props) {
    super(props);

    this.state = {
        email: '',
        password: '',
    }
}

_renderScene(route, navigator) {
    if (route.id === 1) {
        return <PageOne navigator={navigator}/>
    } else if (route.id === 2) {
        return <PageTwo navigator={navigator}/>
    } 
}

} render() { return ( ); } }
&#13;
&#13;
&#13;