【问题标题】:not a function error for imported function不是导入函数的函数错误
【发布时间】:2018-09-25 17:37:27
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript reactjs react-native native


    【解决方案1】:

    您缺少getToken 的映射,就像您为signOut 所做的那样:

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

    这样this.props.getToken 应该在Home 中可用。

    【讨论】:

    • 啊对了,我完全忘记了!谢谢大家!
    猜你喜欢
    • 2019-02-10
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2018-09-09
    • 2017-11-18
    • 2020-05-09
    • 1970-01-01
    相关资源
    最近更新 更多