导入函数不是函数错误

时间:2018-04-16 04:12:00

标签: javascript reactjs react-native native

这可能是一个简单的引用/绑定错误,但我似乎无法弄清楚:

import React from 'react';
var { View, StyleSheet, Alert, AsyncStorage } = require('react-native');

import {Button} from 'react-native-elements'
import {Actions} from 'react-native-router-flux';
import {connect} from 'react-redux';

import styles from "./styles"

import { actions as auth} from "../../../auth/index"
import { actions as home } from "../../index";
import { user } from '../../../auth/scenes/Login/index';

const { getToken } = home;
const { signOut } = auth;

class Home extends React.Component {
    constructor(){
    super();
    this.state = { }

    this.onSignOut = this.onSignOut.bind(this);
    this.onShowData = this.onShowData.bind(this);
}

onSignOut() {
    this.props.signOut(this.onSuccess.bind(this), this.onError.bind(this))
}

onSuccess() {
    Actions.reset("Auth")
}

onError(error) {
    Alert.alert('Oops!', error.message);
}

onShowData(){
 AsyncStorage.getItem("userData").then((value) => {
   this.props.getToken(value); //THIS LINE IS GIVING THE ERROR
   alert(value);
   Actions.pop();
 });
}

render() {
    return (
        <View style={styles.container}>
            <Button
                raised
                borderRadius={4}
                title={'LOG OUT'}
                containerViewStyle={[styles.containerView]}
                buttonStyle={[styles.button]}
                textStyle={styles.buttonText}
                onPress={this.onSignOut}/>
              <Button
                borderRadius={4}
                title={'Show Data'}
                containerViewStyle={[styles.showDataButton]}
                buttonStyle={[styles.button]}
                textStyle={styles.buttonText}
                onPress={this.onShowData}/>
        </View>
    );
}
}

export default connect(null, { signOut })(Home);

奇怪的是,从auth中正确读取了signOut,这是从auth文件夹的acions.js导入的。但是,无法从home识别getToken函数,这是从home foler导入的action.js。 我做了两个完全相同的方式并检查了对这些js文件,目录以及函数导出的所有引用。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您错过了getToken的地图,正如您为signOut所做的那样:

export default connect(null, { signOut, getToken })(Home);
//                                    ^^^^^^^^^^-- added this

这种方式this.props.getToken应该在Home内提供。