也许这是一个愚蠢的问题,但如何从其他功能调用函数?在我的代码中,我可以从构造函数调用函数,但是当我尝试调用函数" showValue()"从其他功能,然后我有这个错误:
undefined不是一个函数(评估' this.showValue()')
我的代码:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
Button,
View
} from 'react-native';
const Realm = require('realm');
let realm;
export default class TestComponent extends Component {
constructor() {
super();
this.state = {
dog:'',
all:'',
};
this.startDB();
}
startDB(){
realm = new Realm({
schema: [{name: 'Dog', properties: {name: 'string'}}]
});
}
adValue(e){
realm.write(() => {
realm.create('Dog', {name: e.nativeEvent.text});
});
}
showValue(){
let all =realm.objects('Dog');
let show='';
all.forEach((name)=>{
show+=name.name+" ";
});
this.setState({
all:show
})
};
remove(){
this.showValue();
}
render() {
return (
<View>
<Text>
Count of Dogs in Realm: {realm.objects('Dog').length}
</Text>
<Button
color={'royalblue'}
onPress={this.remove}
title="Remove"
/>
<Button
color={'brown'}
onPress={this.showValue.bind(this)}
title="Show"
/>
<TextInput
placeholder={'dodaj psa'}
onSubmitEditing={(e)=>this.adValue(e)}
/>
<Text>{this.state.dog}</Text>
<Text>{this.state.all}</Text>
</View>
)
}
}
AppRegistry.registerComponent('TestComponent', () => TestComponent);
为什么我无法调用此功能? 谢谢。