0

i just implemented the checkbox component from native base in my react native app. Every time i check/uncheck the checkbox, some variable change its status (true or false) and then stored. Based on that value stored in the asyncStorage,i can do some changes in the app and it works fine.

The problem I'm facing now is : I need the checkbox to remain checked / unchecked while using the app (even if changing from one page to another) and it can not change while the user didn't change it (it's always going back to the default value once changing the page ) I'm storing the variable as string but apparently checkbox accept only boolean variables. PS: I tried to store the status as boolean but it's not working Here is the code :

constructor(props) {
        super(props);
        this.state ={ status: false }
};

 toggleStatus() 
{
        this.setState({
            status: !this.state.status
        });
     AsyncStorage.setItem("myCheckbox",JSON.stringify(this.state.status));
}

render(){
return(

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

);}

I even tried to change the status like this :

componentWillMount(){
        AsyncStorage.getItem('myCheckbox').then((value) => {
            JSON.parse(value)
            this.setState({
                status: value
            });
            if (this.state.status == 'false') {
                this.setState({
                    status: false
                });
            }
            else if (this.state.status == 'true') {
                this.setState({
                    status: true
                });
            }
            else {
                this.setState({
                    status: false
                });
            }   
        });

    }

But, it keeps throwing a warning : enter image description here

user3521011
  • 1,481
  • 5
  • 20
  • 35

1 Answers1

0

I replicated your code (in: https://snack.expo.io/SJSzZzvbW) and didn't find any reason on it why the type isn't boolean. Do you manipulate state anywhere else in your code?

Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
  • thank you @zvona yes i'm that same "status" to control whether images are being displayed or not in the whole app .. so yes i'm using that status very much but i only change it's value in one page ... you are using button but for me i have to use checkbox and keep it checked or not even if i change the page. The result i'm getting that it changes the status every time i check/uncheck it but if i change the page it always goes back to the default value – user3521011 May 27 '17 at 15:58