thewei / react-native-store

A simple database base on react-native AsyncStorage.
https://github.com/thewei/react-native-store
Other
569 stars 74 forks source link

[ANDROID] Methods work but the effect is not seen instantly #45

Closed mihir0x69 closed 8 years ago

mihir0x69 commented 8 years ago

This is my code. When I call add() method, the data gets stored but it can be seen after I perform add() once again.

var React = require('react-native');

var {
    StyleSheet,
    View,
    Text
} = React;

var Store = require('react-native-store');

var DB = {
    foo: Store.model('foo')
};

module.exports = React.createClass({
    getInitialState: function(){
        return{
            items: null,
            string: ''
        }
    },
    render: function(){
        return(
            <View style={styles.container}>
                <Text onPress={this.onButtonPress}>Add Item</Text>
                <Text onPress={this.destroyData}>Destroy</Text>
                <Text>{this.state.string}</Text>
            </View>
        );
    },
    onButtonPress: function(){
        DB.foo.add({
            fname: 'Mihir',
            lname: 'Karandikar'
        });
        DB.foo.find()
            .then((response) => this.setState({ items: response, string: JSON.stringify(response) }));
        console.log(this.state.items);
    },
    destroyData: function(){
        DB.foo.destroy();       
    },
});

var styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#eaeaea',
        alignItems: 'center',
        justifyContent: 'center'
    }
});

When I console.log this shows up:

adding item...
null
adding item...
null
adding item...
[Object]0: Object 
length: 1
__proto__: Array[0]
mihir0x69 commented 8 years ago

It was happening because of setState() method. Closing. And thanks for this cool repo.