css - React Native:使用 AsyncStorage 和 State 动态更改样式
问题描述
我想在我的应用程序中实现暗模式和黑色模式,我拥有它的方式是用户从我希望将状态更新到所有类的一个类切换到暗/黑色模式,切换类为接着:
外观切换类
state = {
BlackModeValue: null,
DarkModeValue: null
};
componentDidMount = () => {
AsyncStorage.getItem('DarkModeValue').then(value => this.setState({ DarkModeValue: JSON.parse(value) }));
AsyncStorage.getItem('BlackModeValue').then(value => this.setState({ BlackModeValue: JSON.parse(value) }));
};
//AsyncStorage.setItem .........
render() {
return (
<ScrollView style={[ styles.View , this.state.DarkModeValue ? darkmode.ASView : null || this.state.BlackModeValue ? blackmode.ASView : null ]}>
<SettingsList borderColor='#c8c7cc' defaultItemSize={50}>
<SettingsList.Item
hasSwitch={true}
switchState={this.state.DarkModeValue}
switchOnValueChange={//Goes to asyncStorage.setItem method}
title='Dark Mode'
/>
<SettingsList.Item
hasSwitch={true}
switchState={this.state.BlackModeValue}
switchOnValueChange={//Goes to asyncStorage.setItem method}
title='Black Mode'
/>
</SettingsList>
</ScrollView>
);
}
}
然后在我想要 .getItem 并更改状态的类(即 SettingsScreen.js,这是导航到 AppearanceToggle 的屏幕)中,如下所示:
state = {
switchValue: false,
rated: false,
DarkModeValue:null,
BlackModeValue:null,
};
componentDidMount = () => {
AsyncStorage.getItem('DarkModeValue').then(value => this.setState({ DarkModeValue: JSON.parse(value) }));
AsyncStorage.getItem('BlackModeValue').then(value => this.setState({ BlackModeValue: JSON.parse(value) }));
};
render() {
return (
<ScrollView style={[ styles.View , this.state.DarkModeValue ? styles.DMView : null || this.state.BlackModeValue ? styles.BMView : null ]}>
..........
</ScrollView>
我遇到的问题是,当我更改开关时,它会立即影响 AppearanceToggleScreen 类,但不会影响 SettingsScreen,除非我刷新应用程序。有没有办法让所有人都立即受到影响?
解决方案
也许传播它的最好方法是监听你AppComponent
使用的上下文或根组件的变化。例如
因此,您将创建一个主题上下文,例如:
export const themes = {
blackMode: {
foreground: '#000000',
background: '#eeeeee',
},
darkMode: {
foreground: '#2f4f4ff',
background: '#222222',
},
};
export const ThemeContext = React.createContext(
themes.darkMode // default value
)
;
你的AppearanceToggle
班级会有类似的东西:
import {ThemeContext} from './theme-context';
class ThemedButton extends Component {
render() {
let props = this.props;
let theme = this.context;
return (
<button
{...props}
style={{backgroundColor: theme.background}}
/>
);
}
}
ThemedButton.contextType = ThemeContext;
export default ThemedButton;
然后你AppComponent
可能是
import {ThemeContext, themes} from './theme-context';
import ThemedButton from './themed-button';
function Toolbar(props) {
// Render your customized toolbar here and bind the changeTheme function to it
}
class App extends Component {
constructor(props) {
super(props);
};
componentDidMount = () => {
AsyncStorage.getItem('selectedTheme').then(value => this.setState({ selectedTheme: JSON.parse(value) }));
};
this.toggleTheme = () => {
this.setState(state => ({
theme:
state.theme === themes.darkMode
? themes.blackMode
: themes.darkMode,
}));
};
}
render() {
// The ThemedButton button inside the ThemeProvider
// uses the theme from state while the one outside uses
// the default dark theme
return (
<Page>
<ThemeContext.Provider value={this.state.theme}>
<Toolbar changeTheme={this.toggleTheme} />
</ThemeContext.Provider>
<Section>
<ThemedButton />
</Section>
</Page>
);
}
}
更多阅读
推荐阅读
- android - 无法读取包含 SpannableString 而不是常规字符串的通知
- vba - 使用 Cookie 身份验证和 CSRF 令牌连接到 Web API
- angularjs - 如何使用量角器定位自定义指令中的元素?
- pointers - Golang 中的 *uint 和 uintptr 有什么区别?
- sql - SQL选择列基于左连接后另一列的MIN值
- excel - 粗体 E 列中前 50% 的总和值及其行
- ios - 在 Firebase 实时数据库中存储数据
- javascript - 在 javascript 中传递两个 GET 变量
- kubernetes - 我可以在哪里动手学习 Kubernetes
- vue.js - 如何使另一个vue文件中的抽屉在单击时同步可见