Skip to content

Commit

Permalink
Merge pull request #112 from vietredweb/develop
Browse files Browse the repository at this point in the history
Check JSON get Theme
  • Loading branch information
lunguyenhat authored Jan 13, 2023
2 parents 79c3fbb + 0ca56f5 commit 80788e9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/themes/ThemeContextProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const listThemes = [
{ theme: 'light', color: '#0A083B', className: '' },
{ theme: 'dark', color: '#fff', className: '' },
];

function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
export class ThemesContextProvider extends React.Component {
constructor(props) {
super(props);
Expand All @@ -25,7 +32,7 @@ export class ThemesContextProvider extends React.Component {
localStorage.setItem('theme', JSON.stringify(listThemes[0]));
return currentTheme ?? listThemes[0];
}
return JSON.parse(currentTheme);
return isJson(currentTheme) ? JSON.parse(currentTheme) : listThemes[0];
};

changeTheme = (theme) => {
Expand Down

0 comments on commit 80788e9

Please sign in to comment.