1

Maybe it is a stupid question but how to call function from other function? In my code I can call function from constructor but when I try call function "showValue()" from other function then I have this error:

undefined is not a function (evaluating 'this.showValue()')

My code:

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

Why I can't call this function? Thank you.

Klick
  • 1,418
  • 1
  • 23
  • 45

0 Answers0