javascript - 不是导入函数的函数错误

标签 javascript reactjs react-native native

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

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 foler 导入的 action.js。 我以完全相同的方式进行了这两种操作,并检查了对这些 js 文件、目录以及函数导出的所有引用。 有什么想法吗?

最佳答案

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

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

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

关于javascript - 不是导入函数的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49849537/

相关文章:

javascript - React Native Firebase 搜索 ListView

javascript - 将鼠标悬停在切换两个类上后点击事件问题

javascript - 解析空白以类似于浏览器 View

javascript - 背景图像更改按钮在 Firefox 中的默认样式

javascript - 在页面渲染react-native hooks之前从api获取数据

javascript - react native 导航器渲染方法错误

windows - 我正在尝试生成一个 apk 但出现此错误任务 ':app:bundleReleaseJsAndAssets' 执行失败

javascript - 如何将签名保存到本地驱动器

javascript - React 中上下文发生变化时获取数据

reactjs - 世博会初始化不需要的 git repo