react-native-material-ui icon indicating copy to clipboard operation
react-native-material-ui copied to clipboard

Change theme on the fly?

Open iUltimateLP opened this issue 9 years ago • 5 comments

Hey, thanks for this library! I tried to change the theme at runtime by using this.state.theme in the and setting the state from a button, but it only changes the color of the button I pressed, I need to press every button in my layout so it updated to the correct color. Is there a way of redraw all elements so it takes on the new theme? Thank you!

iUltimateLP avatar Aug 23 '17 20:08 iUltimateLP

@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 avatar Jan 16 '18 21:01 aburai

@aburai we're trying to solve the same problem here, please if you can, post the solution in this issue.

romangetman avatar Jan 25 '18 13:01 romangetman

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 avatar Jan 25 '18 14:01 iUltimateLP

@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 avatar Jan 25 '18 14:01 romangetman

@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.

aburai avatar Jan 28 '18 00:01 aburai