0

I have been facing some issues with the native base checkbox and AsynStorage. In fact, AsynStorage only accepts strings by default BUT can store boolean variables also, I tried to use that method but I get a string stored every time. While the checkbox does only accept boolean variables and throws a warning if I tried to use a string and it does not show the previous state of the checkbox (checked or not ). So, I decided to make my own checkbox using TouchbleOpacity .. So do you guys have any idea how to make it ? Here is the result i want to achieve: enter image description here

So, the purpose is to make a checkbox settings page that controls the style of a text in another page and to get the checkbox as left the previous time, for an example : if I check it , I change the page and go back again to the settings page , I need to find it checked (indicating the previous state) The code is in the settings page :

toggleStatus() {
        this.setState({
            status: !this.state.status
        });
        AsyncStorage.setItem("myCheckbox",JSON.stringify(this.state.status));
    }
// to get the previous status stored in the AsyncStorage 
componentWillMount(){
        AsyncStorage.getItem('myCheckbox').then((value) => {
            this.setState({
                status: value
            });
            if (this.state.status == "false") {
                this.setState({
                    check: false
                });
            }
            else if (this.state.status == "true") {
                this.setState({
                    check: true
                });
            }
            if (this.state.status == null) {
                this.setState({
                    check: false
                });
            }
        }); 
    }
render {
return(
 ...

                            <CheckBox
                                onPress={() => { this.toggleStatus() }
                                checked={ this.state.check }/>

)}

In other page :

componentDidMount(){
        AsyncStorage.getItem('myCheckbox').then((value) => {
            JSON.parse(value)
            this.setState({
                status: value
            });

        });

    }

This code change the status after TWO clicks and I don't know why and i get this weird output in the console, every time I click the checkbox enter image description here

user3521011
  • 1,481
  • 5
  • 20
  • 35
  • I think it may be better to have a look at the way you store/retrieve data from AsyncStorage, rather than re-inventing the wheel for the check box. I would store data through `JSON.stringify(myVal)`, and retrieve with `JSON.parse(fromStorage)` - that way you can store complex objects and maintain types. – Andrew Breen Jun 01 '17 at 00:55

1 Answers1

0

If you take a look at AsyncStorage documentation, you can see that, in fact, the method getItem() will always return a string (inside the promise).

For the problem with the AsyncStorage you should consider trying to parse this string returned to a boolean using this method explained here and then use this parsed value inside the native base checkbox.

But if you want to do your own component, try doing something like this:

export default class Checkbox extends Component {
    constructor(){
        super();
        this.state = { checked: false }
    }

    render(){
        return(
        <TouchableOpacity
        onPress={()=>{
            this.setState({ checked : !this.state.checked });
        }}    
        >
            <Image source={this.state.checked ? require('checkedImagePath') : require('uncheckedImagePath')} />
        </TouchableOpacity>
        );
    }
}

You will need to set some style to this image to configure it the way you want.

-- Based on your edition:

I can't see nothing wrong on your toggleStatus() method in settings page, but try changing your componentWillMount() to this:

componentWillMount(){
    AsyncStorage.getItem('myCheckbox').then((value) => {
        if (value != null){
            this.setState({
                check: (value == 'true')
            });
        }
    }); 
}

However in the other page the line you do JSON.parse(value) is doing nothing, once you are not storing the result anywhere.

Pedro Neri
  • 395
  • 1
  • 5
  • 17
  • hey .. thank you actually i did to store the status as a string (which i did) and then to convert it to string ..BUT it's nit working and I don't know why .. I also tried your second solution and it's not changing the status instantly and it always goes back to the checked picture...Please check my edit in the question to see what i tried . – user3521011 Jun 01 '17 at 13:53