不是导入函数的函数错误
not a function error for imported function
这可能是一个简单的 reference/binding 错误,但我似乎无法弄清楚:
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
内使用。
这可能是一个简单的 reference/binding 错误,但我似乎无法弄清楚:
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
内使用。