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?