react native theme switch with redux disappear when app refresh

Viewed 35

I'm a fairly new app developer in react native. In my app, I've created custom checkbox that works properly well. And I want to switch themes with these checkboxes. However,I worked with redux state, and that's switched themes there is no problem. But when I rerun app, selected theme disappears. Please help me!

import { SET_SWITCH_THEME } from "./actions";//themeReducer.js

  const INITIAL_STATE={
      theme: ''
  }

  export function themeReducer(state=INITIAL_STATE,action){
      switch (action.type){
          case 'SET_SWITCH_THEME':
              return{...state,theme:action.payload};
          default:
              return state;
      }
  }

   
  export const SET_SWITCH_THEME='SET_SWITCH_THEME';//actions.js
  export const switchTheme= (theme) =>{
    return{type:SET_SWITCH_THEME, payload:theme}
  }

    
  import {themeReducer} from "./themeReducer";//rootReducer.js
  export default combineReducers({
     themeState:themeReducer,
  })

    
   import {createStore,applyMiddleware} from "redux";//store
   import thunk from "redux-thunk";
   import rootReducer from "./rootReducer";
   const store=createStore(rootReducer,applyMiddleware(thunk));
   export default store;

    
   const App: () => Node = () => {//App.js
     useEffect(() => {
         SplashScreen.hide();
         requestUserPermission().then(r=>{});
         NotificationListener();
       },
       [])
       return (
       <Provider store={store}>
       <MainNavigator />
       </Provider>
     );
   };

    
   import {useDispatch, useSelector} from "react-redux";//Settings.js (switched theme in this screen)
   import {switchTheme} from "../store/redux/actions";
   import CustomCheckBox from "../components/CheckBox"; //my custom checkbox component
   import AsyncStorage from "@react-native-async-storage/async-storage";

   const dispatch=useDispatch();
   const {theme}=useSelector(state=>state.themeState);

   const [checked, setChecked] = useState('default');

   const changeTheme=async(mode)=>{
       setChecked(mode);
       dispatch(switchTheme(mode));
       await AsyncStorage.setItem('@theme', JSON.stringify(mode));
       setModalVisible(!modalVisible);
   }

   <View style={{ justifyContent:'center', alignItems:'center', flexDirection:'row', padding:3, flex:1}}>
     <CustomCheckBox  color={'#fff'} text="Default Theme" status={checked === 'default' ? 'checked' : 'unchecked'} onChange={()=> changeTheme('default')}/>
     <CustomCheckBox  color={'#fff'} text="Koyu Mod" status={checked === 'dark' ? 'checked' : 'unchecked'} onChange={()=>temaSec('dark')}/>
   </View>//i want switch themes with custom checkbox like this

    
   <View style={theme==='dark'?styles.rootContainer:defaultStyles.rootContainer}>//Also, there is a components withs switch theme styles conditionally
   </View>
1 Answers

See as far i see youre just storing in async storage and not fetching it, on reload and refresh, redux data gets refreshed,

2 ways to do it, you can either use AsyncStorage to store that particular theme and on next time just check whether anything was set, and then ovveride the default value like :

import {useDispatch, useSelector} from "react-redux";//Settings.js (switched theme in this screen)
   import {switchTheme} from "../store/redux/actions";
   import CustomCheckBox from "../components/CheckBox"; //my custom checkbox component
   import AsyncStorage from "@react-native-async-storage/async-storage";

   const dispatch=useDispatch();
   const {theme}=useSelector(state=>state.themeState);

   const [checked, setChecked] = useState('default');

   const changeTheme=async(mode)=>{
       setChecked(mode);
       dispatch(switchTheme(mode));
       await AsyncStorage.setItem('@theme', JSON.stringify(mode));
       setModalVisible(!modalVisible);
   }
   //this is added
   const checkForAlreadyTheme = useCallback(async() => {
   const result = await AsyncStorage.getItem('@theme');
   
   if(result){
   dispatch(switchTheme(result));
   }
   
   },[])
   //this is added
   useEffect(() =>{
   
   checkForAlreadyTheme()
   
   },[checkForAlreadyTheme])

   <View style={{ justifyContent:'center', alignItems:'center', flexDirection:'row', padding:3, flex:1}}>
     <CustomCheckBox  color={'#fff'} text="Default Theme" status={checked === 'default' ? 'checked' : 'unchecked'} onChange={()=> changeTheme('default')}/>
     <CustomCheckBox  color={'#fff'} text="Koyu Mod" status={checked === 'dark' ? 'checked' : 'unchecked'} onChange={()=>temaSec('dark')}/>
   </View>//i want switch themes with custom checkbox like this

    
   <View style={theme==='dark'?styles.rootContainer:defaultStyles.rootContainer}>//Also, there is a components withs switch theme styles conditionally
   </View>

Hope this helps, also you can use redux-persist , which will save the whole reducer state in async storage for your

Related