componentDidMount leaves me out the scope of a variable

0

I have a class Database :

class Database  { 
    make(){
        return 'ok';
    }
}

export default new Database();

In my App component I import it in the following way:

import db from 'path/to/Database';

export default class App extends Component {

    componentDidMount(){
        db.make(); // ERROR
    }

    handlePress = () => {
        db.make(); // Me devuelve correctamente el 'ok'
    }

    render() {
        return (
            <View style={{ marginTop: 20 }} >
                <Button
                    onPress={this.handlePress}
                />
            </View>
        )
    }
}

If I use the methods of the class Database with the object db in buttons and outside the life cycle of the component App , everything works without problems, but if I use it within the method componentDidMount() of the component throws me the following error

  

TypeError: TypeError: Can not read property 'make' of undefined

How can I correct it?

    
asked by Jorius 27.07.2018 в 20:51
source

1 answer

0

It should work.

I did a project in Expo ( link ) where I do the same thing that is mentioned in the example and it works correctly: it shows a alert with what returns db.make() .

Did you check that there is no typing error?

    
answered by 15.08.2018 в 19:33