[英]this.props.function() is undefined in React app?
我目前正在构建一个react-redux应用程序,当我单击“保存”按钮时,该应用程序应在actions / index.js中调用函数addToSaved。 但是,我得到的错误是“ this.props.addToSaved未定义”而不是函数。 我在actions / index.js中定义了函数,但想知道是否遗漏了其他东西。 这是我目前正在做的事情:
在components / MainView.js中:
'use strict';
import { addToSaved } from "../actions"
var React = require('react-native');
var {
StyleSheet,
Image,
View,
Text,
Component,
TouchableHighlight
} = React;
class MainView extends Component {
onSearchPressed() {
console.log('search pressed');
this.props.addToSaved();
}
render() {
return (
<View style={styles.container}>
<Image style={styles.image}
source={{uri: pic.img_url}} />
<TouchableHighlight style = {styles.button}
onPress={this.onSearchPressed.bind(this)}
underlayColor='#99d9f4'>
<Text style = {styles.buttonText}>Save</Text>
</TouchableHighlight>
</View>
);
}
}
module.exports = MainView;
在actions / index.js中:
var actions = exports = module.exports
exports.ADD_SAVED = 'ADD_SAVED'
exports.addToSaved = function addToSaved() {
console.log('got to ADD_SAVED');
}
您要导入addToSaved
,并期望它出现在this.props
吗?
如果希望它在this.props
上可访问,则需要将其传递到组件实例中 :
<MainView addToSaved={addToSaved} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.