Change theme on the fly?
Hey, thanks for this library!
I tried to change the theme at runtime by using this.state.theme in the
@iUltimateLP hi, have you found a solution yet?
I find a way to change a theme on the fly by using redux navigation. I will look if i find a place in the docs to describe it.
Or please ping me if you want it here in the issue.
@aburai we're trying to solve the same problem here, please if you can, post the solution in this issue.
Hey, I haven't figured it out yet since I abandoned the project because of severous reasons. Still, if anyone figures it out, I'd be happy to hear too!
@iUltimateLP sorry for mentioning you by mistake. @aburai please advise. We've run into a similar problem and it seems to be related to context not being updated or passed down to children properly.
@romangetman ok, short introduction
in my App component i subscribe to the store to listen on changes
componentDidMount() {
const { store } = this;
// check for changes on theme
// set theme name to re-render
store.subscribe(() => {
const state = store.getState();
const { themeName } = state.theme;
this.setState({ themeName });
});
}
in the render method
render() {
const { themeName } = this.state;
return (
<Provider store={this.store}>
<ThemeProvider uiTheme={uiTheme} init={themeName}>
then trigger your reducer to change your theming
in my case i have only a light and a dark theme and extend the ThemeProvider to use it
class ThemeProvider extends Component {
getChildContext() {
return {
uiTheme: getTheme(this.props.uiTheme, this.props.init),
};
}
import lightTheme from './themes/light';
import darkTheme from './themes/dark';
export default function getTheme(theme, init, ...more) {
const initTheme = init === 'dark' ? darkTheme : lightTheme;
theme = merge(initTheme, theme, ...more);
but i think you can put uiTheme on state and change this.
Hope that helps, if not or you have any problems, let me know.