【问题标题】:Fetch -React Native.Error:on functionFetch -React Native.Error:on 函数
【发布时间】:2017-03-21 07:27:53
【问题描述】:

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', { 方法:'发布', 正文: JSON.stringify({ config_name: '默认', 电子邮件: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);
            })
    });
    

    } 使成为() { 返回 ( ); } }

【问题讨论】:

    标签: react-native


    【解决方案1】:

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

    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 ( ); } }

    【讨论】:

    • null 不是对象(评估 this.state.email)在文本输入中输入输入时出现此错误
    • 好吧,在继续之前,我有点想知道为什么当你有 React.Component 可用时你更喜欢 React.createclass 的旧语法(它没有太多额外的优势)
    • 我能知道您的 TextInput 在哪个页面/类上是 PageOne/PageTwo 还是其他什么??
    • pageone...我很确定 onEmailChange(email) { let s = this.state; s.email = 电子邮件; this.setState(s); }
    • 尝试编辑后的代码,我添加了一些 cmets 供您参考,也可以查看 toddmotto.com/react-create-class-versus-component
    猜你喜欢
    • 2022-12-31
    • 2015-09-06
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2016-11-22
    • 2017-07-27
    • 2022-11-12
    • 2018-11-05
    相关资源
    最近更新 更多